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:com.iiordanov.bVNC.RemoteCanvasActivity.java

private void selectColorModel() {

    String[] choices = new String[COLORMODEL.values().length];
    int currentSelection = -1;
    for (int i = 0; i < choices.length; i++) {
        COLORMODEL cm = COLORMODEL.values()[i];
        choices[i] = cm.toString();//from  w  ww  . ja va  2  s . com
        if (canvas.isColorModel(cm))
            currentSelection = i;
    }

    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    ListView list = new ListView(this);
    list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, choices));
    list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    list.setItemChecked(currentSelection, true);
    list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            dialog.dismiss();
            COLORMODEL cm = COLORMODEL.values()[arg2];
            canvas.setColorModel(cm);
            connection.setColorModel(cm.nameString());
            connection.save(database.getWritableDatabase());
            database.close();
            Toast.makeText(RemoteCanvasActivity.this,
                    getString(R.string.info_update_color_model_to) + cm.toString(), Toast.LENGTH_SHORT).show();
        }
    });
    dialog.setContentView(list);
    dialog.show();
}

From source file:com.simplealertdialog.SimpleAlertDialog.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
    setContentView(R.layout.sad__dialog_simple);
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    // Background
    setBackground(R.id.header, mBackgroundTop);
    setBackground(R.id.bar_wrapper, mBackgroundMiddle);
    setBackground(R.id.body, mBackgroundBottom);

    // Title//from ww w. j  a  v a  2 s .  c  om
    if (TextUtils.isEmpty(mTitle)) {
        findViewById(R.id.header).setVisibility(View.GONE);
        findViewById(R.id.bar_wrapper).setVisibility(View.GONE);
        findViewById(R.id.title).setVisibility(View.GONE);
        findViewById(R.id.icon).setVisibility(View.GONE);
        setBackground(R.id.body, mBackgroundFull);
    } else {
        ((TextView) findViewById(R.id.title)).setText(mTitle);
        if (mTitleTextStyle != 0) {
            ((TextView) findViewById(R.id.title)).setTextAppearance(getContext(), mTitleTextStyle);
        }
        if (mIcon > 0) {
            ((ImageView) findViewById(R.id.icon)).setImageResource(mIcon);
            findViewById(R.id.title).setPadding(findViewById(R.id.title).getPaddingLeft() / 2,
                    findViewById(R.id.title).getPaddingTop(), findViewById(R.id.title).getPaddingRight(),
                    findViewById(R.id.title).getPaddingBottom());
        } else {
            findViewById(R.id.icon).setVisibility(View.GONE);
        }
        setBackground(R.id.bar, mTitleSeparatorBackground);
        if (mTitleSeparatorHeight == 0) {
            mTitleSeparatorHeight = getContext().getResources()
                    .getDimensionPixelSize(R.dimen.sad__dialog_title_separator_height);
        }
        FrameLayout.LayoutParams lpBar = new FrameLayout.LayoutParams(getMatchParent(), mTitleSeparatorHeight);
        findViewById(R.id.bar).setLayoutParams(lpBar);
        findViewById(R.id.bar).requestLayout();
        LinearLayout.LayoutParams lpBarWrapper = new LinearLayout.LayoutParams(getMatchParent(),
                mTitleSeparatorHeight);
        findViewById(R.id.bar_wrapper).setLayoutParams(lpBarWrapper);
        findViewById(R.id.bar_wrapper).requestLayout();
    }

    // Message
    if (TextUtils.isEmpty(mMessage)) {
        findViewById(R.id.message).setVisibility(View.GONE);
    } else {
        ((TextView) findViewById(R.id.message)).setText(mMessage);
        if (mMessageTextStyle != 0) {
            ((TextView) findViewById(R.id.message)).setTextAppearance(getContext(), mMessageTextStyle);
        }
    }

    // Custom View
    if (mView != null) {
        LinearLayout group = (LinearLayout) findViewById(R.id.view);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(getMatchParent(),
                LinearLayout.LayoutParams.WRAP_CONTENT);
        group.addView(mView, lp);
    } else {
        findViewById(R.id.view).setVisibility(View.GONE);
    }

    // Custom Adapter
    if (mAdapter != null) {
        ListView list = (ListView) findViewById(R.id.list);
        list.setAdapter(mAdapter);
        if (mSingleChoice) {
            list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
        if (mSingleChoice && 0 <= mCheckedItem && mCheckedItem < mAdapter.getCount()) {
            list.setItemChecked(mCheckedItem, true);
            list.setSelectionFromTop(mCheckedItem, 0);
        }
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListItemListener != null) {
                    mListItemListener.onItemClick(parent, view, position, id);
                }
                dismiss();
            }
        });
    } else {
        findViewById(R.id.list).setVisibility(View.GONE);
    }

    // Positive Button
    boolean hasPositiveButton = false;
    if (mPositiveButtonText != null) {
        hasPositiveButton = true;
        ((TextView) findViewById(R.id.button_positive_label)).setText(mPositiveButtonText);
        if (mButtonTextStyle != 0) {
            ((TextView) findViewById(R.id.button_positive_label)).setTextAppearance(getContext(),
                    mButtonTextStyle);
        }
    }
    if (mPositiveButtonListener != null) {
        hasPositiveButton = true;
        findViewById(R.id.button_positive).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (mPositiveButtonListener != null) {
                    mPositiveButtonListener.onClick(SimpleAlertDialog.this, 0);
                }
                dismiss();
            }
        });
    }
    if (!hasPositiveButton) {
        findViewById(R.id.button_positive).setVisibility(View.GONE);
    }

    // Neutral Button
    boolean hasNeutralButton = false;
    if (mNeutralButtonText != null) {
        hasNeutralButton = true;
        ((TextView) findViewById(R.id.button_neutral_label)).setText(mNeutralButtonText);
        if (mButtonTextStyle != 0) {
            ((TextView) findViewById(R.id.button_neutral_label)).setTextAppearance(getContext(),
                    mButtonTextStyle);
        }
    }
    if (mNeutralButtonListener != null) {
        hasNeutralButton = true;
        findViewById(R.id.button_neutral).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (mNeutralButtonListener != null) {
                    mNeutralButtonListener.onClick(SimpleAlertDialog.this, 0);
                }
                dismiss();
            }
        });
    }
    if (!hasNeutralButton) {
        findViewById(R.id.button_neutral).setVisibility(View.GONE);
    }

    // Negative Button
    boolean hasNegativeButton = false;
    if (mNegativeButtonText != null) {
        hasNegativeButton = true;
        ((TextView) findViewById(R.id.button_negative_label)).setText(mNegativeButtonText);
        if (mButtonTextStyle != 0) {
            ((TextView) findViewById(R.id.button_negative_label)).setTextAppearance(getContext(),
                    mButtonTextStyle);
        }
    }
    if (mNegativeButtonListener != null) {
        hasNegativeButton = true;
        findViewById(R.id.button_negative).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (mNegativeButtonListener != null) {
                    mNegativeButtonListener.onClick(SimpleAlertDialog.this, 1);
                }
                dismiss();
            }
        });
    }
    if (!hasNegativeButton) {
        findViewById(R.id.button_negative).setVisibility(View.GONE);
    }

    if (!hasPositiveButton && !hasNegativeButton) {
        findViewById(R.id.button_divider_top).setVisibility(View.GONE);
        findViewById(R.id.button_divider).setVisibility(View.GONE);
    } else if (!hasPositiveButton || !hasNegativeButton) {
        findViewById(R.id.button_divider).setVisibility(View.GONE);
        setBackground(R.id.button_divider_top, mButtonTopDividerBackground);
    } else {
        setBackground(R.id.button_divider_top, mButtonTopDividerBackground);
        setBackground(R.id.button_divider, mButtonVerticalDividerBackground);
    }
    if (hasNeutralButton) {
        setBackground(R.id.button_divider_neutral, mButtonVerticalDividerBackground);
    } else {
        findViewById(R.id.button_divider_neutral).setVisibility(View.GONE);
    }
}

From source file:com.todotxt.todotxttouch.TodoTxtTouch.java

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    m_swipeList.discardUndo();//from  ww  w .  j  a  v  a  2  s. c  o  m
    l.setItemChecked(position, l.isItemChecked(position));
    showContextActionBarIfNeeded();
}

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

private void setSelectedTask(int index, @NotNull String selectedTask) {
    Log.v(TAG, "Selected task: " + selectedTask);
    Task task = new Task(index, selectedTask);
    int position = m_adapter.getPosition(task);
    if (position != -1) {
        ListView lv = getListView();
        lv.setItemChecked(position, true);
        lv.setSelection(position);//from w ww  .ja  v a2s . co m
    }

}

From source file:com.audiokernel.euphonyrmt.MainMenuActivity.java

private ListView initializeDrawerList() {
    final ListView drawerList = (ListView) findViewById(R.id.left_drawer);
    final DrawerItem[] drawerItems = {
            new DrawerItem(getString(R.string.libraryTabActivity), DrawerItem.Action.ACTION_LIBRARY),

            new DrawerItem(getString(R.string.outputs), DrawerItem.Action.ACTION_OUTPUTS),

            new DrawerItem(getString(R.string.settings), DrawerItem.Action.ACTION_SETTINGS),
            new DrawerItem("Euphony Settings...", DrawerItem.Action.ACTION_LOON_CLIENT),
            new DrawerItem("Euphony File Manager...", DrawerItem.Action.ACTION_LOON_FM),
            new DrawerItem("Euphony Reboot", DrawerItem.Action.ACTION_LOON_REBOOT),
            new DrawerItem("Euphony Shutdown", DrawerItem.Action.ACTION_LOON_SHUTDOWN) };

    // Set the adapter for the list view
    drawerList.setAdapter(new ArrayAdapter<>(this, R.layout.drawer_list_item, drawerItems));
    drawerList.setItemChecked(mOldDrawerPosition, true);

    // Set the list's click listener
    drawerList.setOnItemClickListener(new DrawerItemClickListener());

    return drawerList;
}

From source file:androidVNC.VncCanvasActivity.java

private void selectColorModel() {
    // Stop repainting the desktop
    // because the display is composited!
    vncCanvas.disableRepaints();/*from   www. j  a  va 2s  .  c om*/

    String[] choices = new String[COLORMODEL.values().length];
    int currentSelection = -1;
    for (int i = 0; i < choices.length; i++) {
        COLORMODEL cm = COLORMODEL.values()[i];
        choices[i] = cm.toString();
        if (vncCanvas.isColorModel(cm))
            currentSelection = i;
    }

    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    ListView list = new ListView(this);
    list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, choices));
    list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    list.setItemChecked(currentSelection, true);
    list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            dialog.dismiss();
            COLORMODEL cm = COLORMODEL.values()[arg2];
            vncCanvas.setColorModel(cm);
            connection.setColorModel(cm.nameString());
            connection.save(database.getWritableDatabase());
            //Toast.makeText(VncCanvasActivity.this,"Updating Color Model to " + cm.toString(), Toast.LENGTH_SHORT).show();
        }
    });
    dialog.setOnDismissListener(new OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface arg0) {
            Log.i(TAG, "Color Model Selector dismissed");
            // Restore desktop repaints
            vncCanvas.enableRepaints();
        }
    });
    dialog.setContentView(list);
    dialog.show();
}

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

private void selectAllTasks() {
    ListView lv = getListView();
    int itemCount = lv.getCount();
    for (int i = 0; i < itemCount; i++) {
        // Only check tasks that are not checked yet
        // and skip headers
        // This prevents double counting in the CAB title
        Task t = getTaskAt(i);//from  www. j  a  v a2s .c o  m
        if (t != null && !lv.isItemChecked(i)) {
            lv.setItemChecked(i, true);
        }
    }
}

From source file:org.openremote.android.console.AppSettingsActivity.java

/**
 * Auto discovery servers and add them in a list view.
 * Click a list item and make it as current server.
 * //w  w w .  java 2  s  . c  o m
 * @return the list view
 */
private ListView constructAutoServersView() {
    final ListView lv = new ListView(this);
    lv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 200));
    lv.setPadding(20, 5, 5, 10);
    lv.setBackgroundColor(0);
    lv.setCacheColorHint(0);
    lv.setItemsCanFocus(true);
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    final ArrayAdapter<String> serverListAdapter = new ArrayAdapter<String>(appSettingsView.getContext(),
            R.layout.server_list_item, new ArrayList<String>());
    lv.setAdapter(serverListAdapter);

    new IPAutoDiscoveryServer() {
        @Override
        protected void onProgressUpdate(Void... values) {
            if (progressLayout != null) {
                progressLayout.setVisibility(View.VISIBLE);
            }
        }

        @Override
        protected void onPostExecute(List<String> result) {
            int length = result.size();
            for (int i = 0; i < length; i++) {
                serverListAdapter.add(result.get(i));
            }
            if (length > 0) {
                lv.setItemChecked(0, true);
                currentServer = serverListAdapter.getItem(0);
                AppSettingsModel.setCurrentServer(AppSettingsActivity.this, currentServer);
            }
            if (progressLayout != null) {
                progressLayout.setVisibility(View.INVISIBLE);
            }
            requestPanelList();
            checkAuthentication();
            requestAccess();
        }
    }.execute((Void) null);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            currentServer = (String) parent.getItemAtPosition(position);
            AppSettingsModel.setCurrentServer(AppSettingsActivity.this, currentServer);
            requestPanelList();
            checkAuthentication();
            requestAccess();
        }
    });

    return lv;
}

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

private void updateLists(@NotNull final List<Task> checkedTasks) {
    final ArrayList<String> contexts = new ArrayList<String>();
    Set<String> selectedContexts = new HashSet<String>();
    final TaskCache taskbag = getTaskBag();
    contexts.addAll(Util.sortWithPrefix(taskbag.getContexts(), m_app.sortCaseSensitive(), null));
    for (Task t : checkedTasks) {
        selectedContexts.addAll(t.getLists());
    }/* ww  w. ja  v a 2 s. c  om*/

    @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,
            contexts.toArray(new String[contexts.size()])));
    lv.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    for (String context : selectedContexts) {
        int position = contexts.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_list_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> originalLines = new ArrayList<String>();
            originalLines.addAll(Util.tasksToString(getCheckedTasks()));
            ArrayList<String> items = new ArrayList<String>();
            ArrayList<String> uncheckedItesm = new ArrayList<String>();
            uncheckedItesm.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.addList(item);
                }
            }
            for (String item : uncheckedItesm) {
                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_lists);
    dialog.show();
}