Select multiple items in the ListView
Description
You can allow multiple items in the ListView to be selected.
Example
/* www. j a v a2 s .c o m*/
package com.java2s.myapplication3.app;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
String[] presidents = {
"XML",
"HTML",
"CSS",
"Java",
"Javascript"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView lstView = getListView();
//lstView.setChoiceMode(ListView.CHOICE_MODE_NONE);
//lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, presidents));
}
public void onListItemClick(
ListView parent, View v, int position, long id)
{
Toast.makeText (this,
"You have selected " + presidents [position],
Toast.LENGTH_SHORT).show();
}
}
Note
To programmatically get a reference to the ListView object, you use the getListView() method, which fetches the ListActivity's list view.