Example usage for android.widget ListView getCheckedItemPositions

List of usage examples for android.widget ListView getCheckedItemPositions

Introduction

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

Prototype

public SparseBooleanArray getCheckedItemPositions() 

Source Link

Document

Returns the set of checked items in the list.

Usage

From source file:nl.mpcjanssen.simpletask.util.Util.java

public static ArrayList<String> getCheckedItems(ListView listView, boolean checked) {
    SparseBooleanArray checks = listView.getCheckedItemPositions();
    ArrayList<String> items = new ArrayList<String>();
    for (int i = 0; i < checks.size(); i++) {
        String item = (String) listView.getAdapter().getItem(checks.keyAt(i));
        if (checks.valueAt(i) && checked) {
            items.add(item);//  w  ww. j  av a2 s  .c o  m
        } else if (!checks.valueAt(i) && !checked) {
            items.add(item);
        }
    }
    return items;
}

From source file:org.totschnig.myexpenses.dialog.SelectFromTableDialogFragment.java

@Override
public void onClick(DialogInterface dialog, int which) {
    if (getActivity() == null || mCursor == null) {
        return;/*from w ww  . j a v a2 s  .  c o  m*/
    }
    ListView listView = ((AlertDialog) dialog).getListView();
    SparseBooleanArray positions = listView.getCheckedItemPositions();

    long[] itemIds = listView.getCheckedItemIds();

    if (itemIds.length > 0) {
        ArrayList<String> labelList = new ArrayList<>();
        for (int i = 0; i < positions.size(); i++) {
            if (positions.valueAt(i)) {
                mCursor.moveToPosition(positions.keyAt(i));
                labelList.add(mCursor.getString(mCursor.getColumnIndex(getColumn())));
            }
        }
        onResult(labelList, itemIds);
    }
    dismiss();
}

From source file:org.totschnig.myexpenses.dialog.SelectFromMappedTableDialogFragment.java

@Override
public void onClick(DialogInterface dialog, int which) {
    if (getActivity() == null || mCursor == null) {
        return;/*from   w  w w.  j  a va2s.com*/
    }
    ListView listView = ((AlertDialog) dialog).getListView();
    SparseBooleanArray positions = listView.getCheckedItemPositions();

    long[] itemIds = listView.getCheckedItemIds();

    if (itemIds.length > 0) {
        ArrayList<String> labelList = new ArrayList<>();
        for (int i = 0; i < positions.size(); i++) {
            if (positions.valueAt(i)) {
                mCursor.moveToPosition(positions.keyAt(i));
                labelList.add(mCursor.getString(mCursor.getColumnIndex(KEY_LABEL)));
            }
        }
        ((MyExpenses) getActivity()).addFilterCriteria(getCommand(),
                makeCriteria(TextUtils.join(",", labelList), itemIds));
    }
    dismiss();
}

From source file:com.btmura.android.reddit.app.SubredditActionModeController.java

private String[] getCheckedSubreddits(ListView lv) {
    SparseBooleanArray checked = lv.getCheckedItemPositions();
    String[] subreddits = new String[lv.getCheckedItemCount()];

    int size = checked.size();
    int j = 0;//from   w  ww.  jav a 2s . co  m
    for (int i = 0; i < size; i++) {
        if (checked.valueAt(i)) {
            int position = checked.keyAt(i);
            subreddits[j++] = adapter.getName(position);
        }
    }

    return subreddits;
}

From source file:com.nagopy.android.mypkgs.ApplicationListFragment.java

@SuppressWarnings("unchecked")
@NonNull/*w ww .jav a  2  s.  c  om*/
<T> List<T> getCheckedItemList(@NonNull ListView listView) {
    List<T> checkedItemList = new ArrayList<>();
    SparseBooleanArray checkedItemPositions = listView.getCheckedItemPositions();
    for (int i = 0; i < checkedItemPositions.size(); i++) {
        if (checkedItemPositions.valueAt(i)) {
            checkedItemList.add((T) listView.getItemAtPosition(checkedItemPositions.keyAt(i)));
        }
    }
    return checkedItemList;
}

From source file:com.github.runoshun.in_app_survey.ui.QuestionFragment.java

private void onMultiChoiceItemClick(MultiChoiceQuestion question, ListView listView, int position) {
    SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
    List<Integer> selectedPositions = new ArrayList<>();

    int count = listView.getAdapter().getCount();
    for (int i = 0; i < count; ++i) {
        if (checkedItems.get(i)) {
            selectedPositions.add(i);/*  www  .  j  a v  a 2  s.  c o  m*/
        }
    }

    if (selectedPositions.size() > question.maxChoices) {
        listView.setItemChecked(position, false);
        selectedPositions.remove((Integer) position);
    }

    int[] selected = new int[selectedPositions.size()];
    for (int i = 0; i < selected.length; ++i) {
        selected[i] = selectedPositions.get(i);
    }

    if (selectedPositions.size() >= question.minChoices) {
        question.saveAnswer(selected);
    } else {
        question.saveAnswer(null);
    }
}

From source file:in.rab.bildkort.SentenceFragment.java

public ArrayList<String> getSelected() {
    ArrayList<String> selected = new ArrayList<>();
    ListView listView = getListView();
    SparseBooleanArray items = listView.getCheckedItemPositions();

    if (items == null) {
        return selected;
    }//from   w  ww .j av  a2  s  .c  om

    for (int i = 0; i < mSentences.size(); i++) {
        if (!items.get(i)) {
            continue;
        }

        selected.add(mSentences.get(i).replace("strong>", "em>"));
    }

    return selected;
}

From source file:com.akalipetis.action_mode_list_fragment.ActionModeListFragment.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public int calculateCheckedItems() {
    ListView l = getListView();
    if (Build.VERSION.SDK_INT >= 11)
        return l.getCheckedItemCount();
    SparseBooleanArray checkedItems = l.getCheckedItemPositions();
    if (checkedItems == null)
        return 0;
    int cnt = 0;//from   w ww . ja va2 s. c  o m
    for (int i = 0, lim = checkedItems.size(); i < lim; ++i) {
        int key = checkedItems.keyAt(i);
        if (checkedItems.get(key, false))
            cnt++;
    }
    return cnt;
}

From source file:com.akalipetis.fragment.ActionModeListFragment.java

public int calculateCheckedItems() {
    ListView l = getListView();
    if (Build.VERSION.SDK_INT >= 11)
        return l.getCheckedItemCount();
    SparseBooleanArray checkedItems = l.getCheckedItemPositions();
    if (checkedItems == null)
        return 0;
    int cnt = 0;//from  w ww . jav  a 2  s.  co m
    for (int i = 0, lim = checkedItems.size(); i < lim; ++i) {
        int key = checkedItems.keyAt(i);
        if (checkedItems.get(key, false))
            cnt++;
    }
    return cnt;
}

From source file:ca.rmen.android.networkmonitor.app.prefs.SelectFieldsActivity.java

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    Log.v(TAG, "onListItemClick: clicked on view " + v + " at position " + position + " with id " + id);
    View okButton = findViewById(R.id.ok);
    SparseBooleanArray checkedItemPositions = l.getCheckedItemPositions();
    for (int i = 0; i < checkedItemPositions.size(); i++) {
        if (checkedItemPositions.get(checkedItemPositions.keyAt(i))) {
            okButton.setEnabled(true);//  ww  w .  j  a va2s  .c  o  m
            return;
        }
    }
    okButton.setEnabled(false);
}