Multi-column ListActivity
package app.test;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Test extends ListActivity
{
private static final String TAG = "ListViewActivity3";
private ListView lv = null;
private Cursor cursor = null;
private int idCol = -1;
private int nameCol = -1;
private int notesCol = -1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = getListView();
cursor = managedQuery(People.CONTENT_URI,
null, null, null, People.NAME);
String[] cols = new String[]{People.NAME};
idCol = cursor.getColumnIndex(People._ID);
nameCol = cursor.getColumnIndex(People.NAME);
notesCol = cursor.getColumnIndex(People.NOTES);
int[] views = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_multiple_choice,
cursor, cols, views);
this.setListAdapter(adapter);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
public void doClick(View view) {
int count=lv.getCount();
SparseBooleanArray viewItems = lv.getCheckedItemPositions();
for(int i=0; i<count; i++) {
if(viewItems.get(i)) {
cursor.moveToPosition(i);
long id = cursor.getLong(idCol);
String name = cursor.getString(nameCol);
String notes = cursor.getString(notesCol);
Log.v(TAG, name + " is checked. Notes: " + notes +
". Position = " + i + ". Id = " + id);
}
}
}
}
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is at /res/layout/list.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:id="@android:id/list"
android:layout_width="fill_parent" android:layout_height="0dip"
android:layout_weight="1" />
<Button android:id="@+id/btn" android:onClick="doClick"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Submit Selection" />
</LinearLayout>
Related examples in the same category