Using SimpleCursorAdapter and ListView : CursorAdapter « UI « Android






Using SimpleCursorAdapter and ListView

  
package app.test;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class Test extends Activity {
 
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    String dataPath = getIntent().getData().toString();
    
    final Uri data = Uri.parse(dataPath + "people/");
    final Cursor c = managedQuery(data, null, null, null, null);
        
    String[] from = new String[] {People.NAME};
    int[]  to = new int[] { R.id.itemTextView };
        
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(),
                                                          R.layout.listitemlayout,
                                                          c, from, to);
    ListView lv = (ListView)findViewById(R.id.contactListView);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
  
          c.moveToPosition(pos);
          int rowId = c.getInt(c.getColumnIndexOrThrow("_id"));
          Uri outURI = Uri.parse(data.toString() + rowId);
          Intent outData = new Intent();
          outData.setData(outURI);
          setResult(Activity.RESULT_OK, outData);
          finish();
        }
        
      });   
  }

}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView
    android:id="@+id/itemTextView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:padding="10px"
    android:textSize="16px"
    android:textColor="#FFF"
  />
</LinearLayout>

   
    
  








Related examples in the same category

1.Using SimpleCursorAdapter and GridView
2.extends CursorAdapter implements Filterable
3.Using SimpleCursorAdapter