Multi-column ListActivity : ListActivity « UI « Android






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

1.A list view example where the data for the list comes from an array of strings.
2.A list view example where the data comes from a cursor.
3.Using ListActivity
4.SimpleCursorAdapter and ListActivity
5.Get ListActivity selected index
6.A list view example with separators.
7.List item click event
8.Dynamic List item
9.Self Wrapper for List
10.Static text for List view
11.List selection event
12.Simple List single choice
13.To do list app
14.Scale listener
15.Weather List Widget
16.Demonstrates expandable lists backed by Cursors
17.Demonstrates expandable lists backed by a Simple Map-based adapter
18.Demonstrates the using a list view in transcript mode
19.Demonstrates how a list can avoid expensive operations during scrolls or flings.
20.Demonstrates how to write an efficient list adapter.
21.A list view where the last item the user clicked is placed in the "activated" state, causing its background to highlight.
22.Calculate the minimum and maximum values out of a list of doubles.
23.Bounded Linked List
24.Adapter that simply returns row views from a list.
25.Diary app