Android contact list (adapter)
(noob question) Hello,
I am using this code to make a list of all contacts and display the phone number.
Currently I just want to display the name, but I am facing difficulties and I get a force close error.
ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.beta.cphonebook/.CPhonebook }
The code I am using is this:
PhoneList.java:
package com.beta.cphonebook;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.content.ContentUris;
import android.net.Uri;
import android.database.Cursor;
public class PhoneList extends ListActivity {
private SimpleCursorAdapter myAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
ArrayList<String> nlist = new ArrayList<String>();
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
nlist.add(name);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//Query phone here. Covered next
}
}
}
String[] nlistString = new String[nlist.size()];
nlist.toArray(nlistString);
Arrays.sort(nlistString);
int[] mapto = new int[] {R.id.contact_name};
//------------------------------------------------------------------
//This is where I get the error. While trying to use the list adapter
ListAdapter mAdapter = new SimpleCursorAdapter(this,R.layout.phonelist,cur,test,mapto);
this.setListAdapter(mAdapter);
//------------------------------------------------------------------
}
}
phonelist.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:src="@dra开发者_StackOverflowwable/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: "
/>
<TextView android:id="@+id/contact_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number: "
/>
<TextView android:id="@+id/contact_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I know it should be something very simple, but for the past 3 hours, I just can't find it!!
instead of using the SimpleCursorAdapter class you can use ArrayAdapter....because u need to sort the name...and it is after iterating cursor.... so your code can be like the following
private ArrayAdapter<String> myAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
ArrayList<String> nlist = new ArrayList<String>();
if (cur.getCount() > 0) {
if(cur.moveToFirst()) {
do {
Log.i("Test", "---------------------count-------------------" + cur.getCount());
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
nlist.add(name);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//Query phone here. Covered next
//String num = cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.NUMBER));
}
}while (cur.moveToNext()) ;
}
}
String[] nlistString = new String[nlist.size()];
nlist.toArray(nlistString);
Arrays.sort(nlistString);
myAdapter = new ArrayAdapter<String>(this, R.layout.phonelist, R.id.contact_name, nlistString);
this.setListAdapter(myAdapter);
}
}
精彩评论