Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>See if the code helps you(it's basically a <code>ListActivity</code> with a custom adapter to hold the status of checked items(+ different background)):</p> <pre><code>public class CABSelection extends ListActivity { private ArrayList&lt;String&gt; mItems = new ArrayList&lt;String&gt;(); private SelectionAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); for (int i = 0; i &lt; 24; i++) { mItems.add("Name" + i); } // R.layout.adapters_cabselection_row is a LinearLayout(with green // background(#99cc00)) that wraps an ImageView and a TextView mAdapter = new SelectionAdapter(this, R.layout.adapters_cabselection_row, R.id.the_text, mItems); setListAdapter(mAdapter); getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() { private int nr = 0; @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.cabselection_menu, menu); return true; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { StringBuilder sb = new StringBuilder(); Set&lt;Integer&gt; positions = mAdapter.getCurrentCheckedPosition(); for (Integer pos : positions) { sb.append(" " + pos + ","); } switch (item.getItemId()) { case R.id.edit_entry: Toast.makeText(CABSelection.this, "Edited entries: " + sb.toString(), Toast.LENGTH_SHORT).show(); break; case R.id.delete_entry: Toast.makeText(CABSelection.this, "Deleted entries : " + sb.toString(), Toast.LENGTH_SHORT).show(); break; case R.id.finish_it: nr = 0; mAdapter.clearSelection(); Toast.makeText(CABSelection.this, "Finish the CAB!", Toast.LENGTH_SHORT).show(); mode.finish(); } return false; } @Override public void onDestroyActionMode(ActionMode mode) { nr = 0; mAdapter.clearSelection(); } @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { if (checked) { nr++; mAdapter.setNewSelection(position, checked); } else { nr--; mAdapter.removeSelection(position); } mode.setTitle(nr + " rows selected!"); } }); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { l.setItemChecked(position, !mAdapter.isPositionChecked(position)); } private class SelectionAdapter extends ArrayAdapter&lt;String&gt; { private HashMap&lt;Integer, Boolean&gt; mSelection = new HashMap&lt;Integer, Boolean&gt;(); public SelectionAdapter(Context context, int resource, int textViewResourceId, List&lt;String&gt; objects) { super(context, resource, textViewResourceId, objects); } public void setNewSelection(int position, boolean value) { mSelection.put(position, value); notifyDataSetChanged(); } public boolean isPositionChecked(int position) { Boolean result = mSelection.get(position); return result == null ? false : result; } public Set&lt;Integer&gt; getCurrentCheckedPosition() { return mSelection.keySet(); } public void removeSelection(int position) { mSelection.remove(position); notifyDataSetChanged(); } public void clearSelection() { mSelection = new HashMap&lt;Integer, Boolean&gt;(); notifyDataSetChanged(); } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views v.setBackgroundColor(Color.parseColor("#99cc00")); //default color if (mSelection.get(position) != null) { v.setBackgroundColor(Color.RED);// this is a selected position so make it red } return v; } } } </code></pre> <p>The <code>R.layout.adapters_cabselection_row</code> is a custom layout for the row(a very simple one) with a green background:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#99cc00" &gt; &lt;ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /&gt; &lt;TextView android:id="@+id/the_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="17sp" android:textStyle="bold" /&gt; &lt;/LinearLayout&gt; </code></pre> <p><code>R.menu.cabselection_menu</code> is a menu file with 3 options(edit, delete, finish the CAB) which don't do anything except pop a <code>Toast</code> with a message regarding the rows selected:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;menu xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;item android:id="@+id/edit_entry" android:icon="@android:drawable/ic_menu_edit" android:title="Edit!"/&gt; &lt;item android:id="@+id/delete_entry" android:icon="@android:drawable/ic_menu_delete" android:title="Delete!"/&gt; &lt;item android:id="@+id/finish_it" android:icon="@android:drawable/ic_menu_crop" android:title="Get me out!"/&gt; &lt;/menu&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload