Android联系人列表(适配器)



(新手问题)你好,

我正在使用这个代码制作所有联系人的列表并显示电话号码。

目前我只想显示名称,但是我遇到了困难,我得到了一个强制关闭错误。

ActivityManager: Starting: Intent {act=android.intent.action.MAIN猫= [android.intent.category.LAUNCHER]cmp = com.beta.cphonebook/。CPhonebook}

我使用的代码是:

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="@drawable/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>

我知道它应该是非常简单的东西,但是在过去的3个小时里,我就是找不到它!

可以使用ArrayAdapter....代替SimpleCursorAdapter类因为你需要对名字进行排序…这是在游标....迭代之后所以你的代码可以像下面这样

 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);
}

}

最新更新