// **Step 1**: setAdapter to your listview.
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, GENRES));
// **Step 2**: set choice mode for listview .The second line of below code represents which checkbox should be checked.
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemChecked(2, true);
listView.setOnItemClickListener(this);
private static String[] GENRES = new String[] {
"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
"Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
};
//**Step 3:** Checked views are returned in SparseBooleanArray, so you might use the below code to get key or values.The below sample are simply displayed selected names in a single String.
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int arg2, long arg3)
{
SparseBooleanArray sp=getListView().getCheckedItemPositions();
String str="";
for(int i=0;i<sp.size();i++)
{
str+=GENRES[sp.keyAt(i)]+",";
}
Toast.makeText(this, ""+str, Toast.LENGTH_SHORT).show();
}
Dies funktioniert für me.but Wenn ich listitem wird deaktivieren, wie das Element von str zu entfernen. str + = GENRES [sp.keyAt (i)] + ",";Listview mit Multiselect
Kann mir bitte jemand helfen.
Da Sie nur 'getCheckedItemPositions();' verwenden, sollte das automatisch funktionieren –
Nein @Tim das funktioniert nicht – ananya