Example usage for android.widget ListView setItemChecked

List of usage examples for android.widget ListView setItemChecked

Introduction

In this page you can find the example usage for android.widget ListView setItemChecked.

Prototype

public void setItemChecked(int position, boolean value) 

Source Link

Document

Sets the checked state of the specified position.

Usage

From source file:nl.mpcjanssen.simpletask.Simpletask.java

private void updateTags(@NotNull final List<Task> checkedTasks) {
    final ArrayList<String> projects = new ArrayList<String>();
    Set<String> selectedProjects = new HashSet<String>();
    final TaskCache taskbag = getTaskBag();
    projects.addAll(Util.sortWithPrefix(taskbag.getProjects(), m_app.sortCaseSensitive(), null));
    for (Task t : checkedTasks) {
        selectedProjects.addAll(t.getTags());
    }//from  w  w w . j ava2  s .c  o  m

    @SuppressLint("InflateParams")
    View view = getLayoutInflater().inflate(R.layout.tag_dialog, null, false);
    final ListView lv = (ListView) view.findViewById(R.id.listView);
    lv.setAdapter(new ArrayAdapter<String>(this, R.layout.simple_list_item_multiple_choice,
            projects.toArray(new String[projects.size()])));
    lv.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    for (String context : selectedProjects) {
        int position = projects.indexOf(context);
        if (position != -1) {
            lv.setItemChecked(position, true);
        }
    }

    final EditText ed = (EditText) view.findViewById(R.id.editText);
    m_app.setEditTextHint(ed, R.string.new_tag_name);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(view);

    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            ArrayList<String> items = new ArrayList<String>();
            ArrayList<String> originalLines = new ArrayList<String>();
            originalLines.addAll(Util.tasksToString(checkedTasks));
            ArrayList<String> uncheckedItems = new ArrayList<String>();
            uncheckedItems.addAll(Util.getCheckedItems(lv, false));
            items.addAll(Util.getCheckedItems(lv, true));
            String newText = ed.getText().toString();
            if (!newText.equals("")) {
                items.add(ed.getText().toString());
            }
            for (String item : items) {
                for (Task t : checkedTasks) {
                    t.addTag(item);
                }
            }
            for (String item : uncheckedItems) {
                for (Task t : checkedTasks) {
                    t.removeTag("+" + item);
                }
            }
            finishActionmode();
            m_app.getTaskCache(null).modify(originalLines, checkedTasks, null, null);
        }
    });
    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User cancelled the dialog
        }
    });
    // Create the AlertDialog
    AlertDialog dialog = builder.create();
    dialog.setTitle(R.string.update_tags);
    dialog.show();
}

From source file:universe.constellation.orion.viewer.OrionViewerActivity.java

public void initRotationScreen() {
    //if (getDevice() instanceof EdgeDevice) {
    if (false) {/*from w  w  w.j a  v  a2s.c  o m*/
        final RadioGroup rotationGroup = (RadioGroup) findMyViewById(R.id.rotationGroup);

        rotationGroup.check(R.id.rotate0);

        if (Device.Info.NOOK2) {
            RadioButton r0 = (RadioButton) rotationGroup.findViewById(R.id.rotate0);
            RadioButton r90 = (RadioButton) rotationGroup.findViewById(R.id.rotate90);
            RadioButton r270 = (RadioButton) rotationGroup.findViewById(R.id.rotate270);
            TextView tv = (TextView) findMyViewById(R.id.navigation_title);
            int color = tv.getTextColors().getDefaultColor();
            r0.setTextColor(color);
            r90.setTextColor(color);
            r270.setTextColor(color);
        }

        getSubscriptionManager().addDocListeners(new DocumentViewAdapter() {
            @Override
            public void documentOpened(Controller controller) {
                updateRotation();
            }
        });

        ImageButton apply = (ImageButton) findMyViewById(R.id.rotation_apply);
        apply.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                onApplyAction(true);
                int id = rotationGroup.getCheckedRadioButtonId();
                controller.setRotation(id == R.id.rotate0 ? 0 : id == R.id.rotate90 ? -1 : 1);
            }
        });

        ListView list = (ListView) findMyViewById(R.id.rotationList);
        list.setVisibility(View.GONE);
    } else {
        RadioGroup rotationGroup = (RadioGroup) findMyViewById(R.id.rotationGroup);
        rotationGroup.setVisibility(View.GONE);

        final ListView list = (ListView) findMyViewById(R.id.rotationList);

        //set choices and replace 0 one with Application Default
        boolean isLevel9 = getOrionContext().getSdkVersion() >= 9;
        CharSequence[] values = getResources().getTextArray(
                isLevel9 ? R.array.screen_orientation_full_desc : R.array.screen_orientation_desc);
        CharSequence[] newValues = new CharSequence[values.length];
        for (int i = 0; i < values.length; i++) {
            newValues[i] = values[i];
        }
        newValues[0] = getResources().getString(R.string.orientation_default_rotation);

        list.setAdapter(Device.Info.NOOK2
                ? new Nook2ListAdapter(this, android.R.layout.simple_list_item_single_choice, newValues,
                        (TextView) findMyViewById(R.id.navigation_title))
                : new ArrayAdapter(this, android.R.layout.simple_list_item_single_choice, newValues));

        list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        list.setItemChecked(0, true);

        list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            public void onItemClick(android.widget.AdapterView parent, View view, int position, long id) {
                CheckedTextView check = (CheckedTextView) view;
                check.setChecked(!check.isChecked());
            }
        });

        final CharSequence[] ORIENTATION_ARRAY = getResources().getTextArray(R.array.screen_orientation_full);

        list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            public void onItemClick(android.widget.AdapterView parent, View view, int position, long id) {
                onApplyAction(true);
                String orientation = ORIENTATION_ARRAY[position].toString();
                controller.changeOrinatation(orientation);
            }
        });

        ImageButton apply = (ImageButton) findMyViewById(R.id.rotation_apply);
        apply.setVisibility(View.GONE);
        //            apply.setOnClickListener(new View.OnClickListener() {
        //                public void onClick(View view) {
        //                    onApplyAction(true);
        //                }
        //            });
    }

    ImageButton cancel = (ImageButton) findMyViewById(R.id.rotation_close);
    cancel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            onAnimatorCancel();
            updateRotation();
        }
    });
}

From source file:usbong.android.likha_collection_1.UsbongDecisionTreeEngineActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mTts.isSpeaking()) {
        mTts.stop();/*w w w  . j a  va 2s . c o m*/
    }

    StringBuffer sb = new StringBuffer();
    switch (item.getItemId()) {
    case (R.id.set_language):
        initSetLanguage();
        //refresh the menu options
        supportInvalidateOptionsMenu(); //added by Mike, 20160507
        invalidateOptionsMenu();
        return true;
    case (R.id.speak):
        processSpeak(sb);
        return true;
    case (R.id.settings):
        //20160417
        //Reference: http://stackoverflow.com/questions/16954196/alertdialog-with-checkbox-in-android;
        //last accessed: 20160408; answer by: kamal; edited by: Empty2K12
        final CharSequence[] items = { UsbongConstants.AUTO_NARRATE_STRING, UsbongConstants.AUTO_PLAY_STRING,
                UsbongConstants.AUTO_LOOP_STRING };
        // arraylist to keep the selected items
        selectedSettingsItems = new ArrayList<Integer>();

        //check saved settings
        if (UsbongUtils.IS_IN_AUTO_NARRATE_MODE) {
            selectedSettingsItems.add(UsbongConstants.AUTO_NARRATE);
        }
        if (UsbongUtils.IS_IN_AUTO_PLAY_MODE) {
            selectedSettingsItems.add(UsbongConstants.AUTO_PLAY);
            selectedSettingsItems.add(UsbongConstants.AUTO_NARRATE); //if AUTO_PLAY is checked, AUTO_NARRATE should also be checked
        }
        if (UsbongUtils.IS_IN_AUTO_LOOP_MODE) {
            selectedSettingsItems.add(UsbongConstants.AUTO_LOOP);
        }

        selectedSettingsItemsInBoolean = new boolean[items.length];
        for (int k = 0; k < items.length; k++) {
            selectedSettingsItemsInBoolean[k] = false;
        }
        for (int i = 0; i < selectedSettingsItems.size(); i++) {
            selectedSettingsItemsInBoolean[selectedSettingsItems.get(i)] = true;
        }

        inAppSettingsDialog = new AlertDialog.Builder(this).setTitle("Settings").setMultiChoiceItems(items,
                selectedSettingsItemsInBoolean, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
                        Log.d(">>>", "onClick");

                        if (isChecked) {
                            // If the user checked the item, add it to the selected items
                            selectedSettingsItems.add(indexSelected);
                            if ((indexSelected == UsbongConstants.AUTO_PLAY)
                                    && !selectedSettingsItems.contains(UsbongConstants.AUTO_NARRATE)) {
                                final ListView list = inAppSettingsDialog.getListView();
                                list.setItemChecked(UsbongConstants.AUTO_NARRATE, true);
                            }
                        } else if (selectedSettingsItems.contains(indexSelected)) {
                            if ((indexSelected == UsbongConstants.AUTO_NARRATE)
                                    && selectedSettingsItems.contains(UsbongConstants.AUTO_PLAY)) {
                                final ListView list = inAppSettingsDialog.getListView();
                                list.setItemChecked(indexSelected, false);
                            } else {
                                // Else, if the item is already in the array, remove it
                                selectedSettingsItems.remove(Integer.valueOf(indexSelected));
                            }
                        }

                        //updated selectedSettingsItemsInBoolean
                        for (int k = 0; k < items.length; k++) {
                            selectedSettingsItemsInBoolean[k] = false;
                        }
                        for (int i = 0; i < selectedSettingsItems.size(); i++) {
                            selectedSettingsItemsInBoolean[selectedSettingsItems.get(i)] = true;
                        }
                    }
                }).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        try {
                            InputStreamReader reader = UsbongUtils
                                    .getFileFromSDCardAsReader(UsbongUtils.BASE_FILE_PATH + "usbong.config");
                            BufferedReader br = new BufferedReader(reader);
                            String currLineString;

                            //write first to a temporary file
                            PrintWriter out = UsbongUtils.getFileFromSDCardAsWriter(
                                    UsbongUtils.BASE_FILE_PATH + "usbong.config" + "TEMP");

                            while ((currLineString = br.readLine()) != null) {
                                Log.d(">>>", "currLineString: " + currLineString);
                                if ((currLineString.contains("IS_IN_AUTO_NARRATE_MODE="))
                                        || (currLineString.contains("IS_IN_AUTO_PLAY_MODE="))
                                        || (currLineString.contains("IS_IN_AUTO_LOOP_MODE="))) {
                                    continue;
                                } else {
                                    out.println(currLineString);
                                }
                            }

                            for (int i = 0; i < items.length; i++) {
                                Log.d(">>>>", i + "");
                                if (selectedSettingsItemsInBoolean[i] == true) {
                                    if (i == UsbongConstants.AUTO_NARRATE) {
                                        out.println("IS_IN_AUTO_NARRATE_MODE=ON");
                                        UsbongUtils.IS_IN_AUTO_NARRATE_MODE = true;
                                    } else if (i == UsbongConstants.AUTO_PLAY) {
                                        out.println("IS_IN_AUTO_PLAY_MODE=ON");
                                        UsbongUtils.IS_IN_AUTO_PLAY_MODE = true;
                                        /*                            
                                                                    //if auto_play is ON, auto_narrate is also ON
                                                                    //however, it is possible to have auto_play OFF,
                                                                    //while auto_narrate is ON
                                                                    out.println("IS_IN_AUTO_NARRATE_MODE=ON");
                                                                    UsbongUtils.IS_IN_AUTO_NARRATE_MODE=true;
                                        */
                                    } else if (i == UsbongConstants.AUTO_LOOP) {
                                        out.println("IS_IN_AUTO_LOOP_MODE=ON");
                                        UsbongUtils.IS_IN_AUTO_LOOP_MODE = true;
                                    }
                                } else {
                                    if (i == UsbongConstants.AUTO_NARRATE) {
                                        out.println("IS_IN_AUTO_NARRATE_MODE=OFF");
                                        UsbongUtils.IS_IN_AUTO_NARRATE_MODE = false;
                                    } else if (i == UsbongConstants.AUTO_PLAY) {
                                        out.println("IS_IN_AUTO_PLAY_MODE=OFF");
                                        UsbongUtils.IS_IN_AUTO_PLAY_MODE = false;
                                    } else if (i == UsbongConstants.AUTO_LOOP) {
                                        out.println("IS_IN_AUTO_LOOP_MODE=OFF");
                                        UsbongUtils.IS_IN_AUTO_LOOP_MODE = false;
                                    }
                                }
                            }
                            out.close(); //remember to close

                            //copy temp file to actual usbong.config file
                            InputStreamReader reader2 = UsbongUtils.getFileFromSDCardAsReader(
                                    UsbongUtils.BASE_FILE_PATH + "usbong.config" + "TEMP");
                            BufferedReader br2 = new BufferedReader(reader2);
                            String currLineString2;

                            //write to actual usbong.config file
                            PrintWriter out2 = UsbongUtils
                                    .getFileFromSDCardAsWriter(UsbongUtils.BASE_FILE_PATH + "usbong.config");

                            while ((currLineString2 = br2.readLine()) != null) {
                                out2.println(currLineString2);
                            }
                            out2.close();

                            UsbongUtils.deleteRecursive(
                                    new File(UsbongUtils.BASE_FILE_PATH + "usbong.config" + "TEMP"));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        //  Your code when user clicked on Cancel
                    }
                }).create();
        inAppSettingsDialog.show();
        /*         
        new AlertDialog.Builder(UsbongDecisionTreeEngineActivity.this).setTitle("Settings")
                    .setMessage("Automatic voice-over narration:")
        //               .setView(requiredFieldAlertStringTextView)
        .setPositiveButton("Turn On", new DialogInterface.OnClickListener() {               
          @Override
          public void onClick(DialogInterface dialog, int which) {
             UsbongUtils.isInAutoVoiceOverNarration=true;
          }
        })
        .setNegativeButton("Turn Off", new DialogInterface.OnClickListener() {               
          @Override
          public void onClick(DialogInterface dialog, int which) {
             UsbongUtils.isInAutoVoiceOverNarration=false;
          }
                    }).show();
        */
        return true;
    case (R.id.about):
        new AlertDialog.Builder(UsbongDecisionTreeEngineActivity.this).setTitle("About")
                .setMessage(UsbongUtils.readTextFileInAssetsFolder(UsbongDecisionTreeEngineActivity.this,
                        "credits.txt")) //don't add a '/', otherwise the file would not be found
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).show();
        return true;
    case android.R.id.home: //added by Mike, 22 Sept. 2015
        processReturnToTitleScreenActivity();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}