Fill contact information to ListView : ListView « UI « Android






Fill contact information to 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 ExpandableListView
2.Using ListView
3.Using SimpleAdapter to fill data to ListView
4.Custom cell Renderer for ListView
5.Provide xml layout for ListView Item
6.ListView.CHOICE_MODE_MULTIPLE
7.Use AbsListView OnScrollListener(AbsListView.OnScrollListener), AbsListView#setOnItemScrollListener(AbsListView.OnItemScrollListener)} to display the first letter of the visible range of cheeses.
8.This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL, a.k.a. selection mode on ListView.
9.FileList extends ListView
10.set ListView Height Based On Children
11.Adding a List
12.Lunch List
13.Get Item index in item click event
14.On nothing selected event
15.Create List