Display data ListView listview = (ListView) findViewById(xxx); MyAdapter adapter = new MyAdapter(); listview.setAdapter(adapter);
xxx
Get selected items int i, pos; SparseBooleanArray array; array = listView.getCheckedItemPositions(); for (i = 0; i < array.size(); i++) { pos = array.keyAt(i); System.out.println("the item " + pos + " is selected"); }
Long clickable event
Add android:longClickable="true" to ListView element of layout file
Regieter listener with the following function ListView.setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener)
xxx
Long clickable Context Menu
Add android:longClickable="true" to ListView element of layout file
Invoke registerForContextMenu(getListView()) in Activity
Override the following functions of Activity public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfoIn); public boolean onContextItemSelected(MenuItem item)
Note:
if you called ListView.setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener) for the list view, you must ensure the method onItemLongClick of interface AdapterView.OnItemLongClickListener to return false; ie: public MyItemLongClickedListener implements AdapterView.OnItemLongClickListener { public boolean onItemLongClick(AdapterView> parent, View view, int position, long id) { .... return false; } }
xxx
xxx
Set choice mode listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);