简体   繁体   English

我想获取所有联系人列表(内容提供商)并将其附加到TextView中

[英]I would like to get all the contact list(content provider) and append them into a TextView

I wonder if i am in the right path. 我想知道我走的路是否正确。 The emulator keeps showing the message xxx.app stopped exectedly and ask to force shutdown. 模拟器一直显示消息xxx.app意外停止,并要求强制关闭。 Here are the codes 这是代码

package fypj.ContactList;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.Contacts.People;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ContactList extends Activity {
    ListView ContactsLV;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        ContactsLV = (ListView)findViewById(R.id.ContactsLV);

        populateContactList();
    }
    public void contentProvider(ContentResolver getContentResolver){
        ContentValues values = new ContentValues();
        values.put(Phone.DISPLAY_NAME, "Jaslyn");   
        values.put(Phone.LABEL, "Jaslyn Goh");
        values.put(Phone.STARRED, 1);
        Uri uri = getContentResolver().insert(Phone.CONTENT_URI, values);

        Uri phoneUri = null;
        Uri emailUri = null;
        phoneUri = Uri.withAppendedPath(uri, People.ContactMethods.CONTENT_DIRECTORY);

        values.clear();
        values.put(Phone.TYPE, Phone.TYPE_MOBILE);
        values.put(Phone.NUMBER, "91289161");
        getContentResolver().insert(phoneUri, values);
        emailUri = Uri.withAppendedPath(uri, People.ContactMethods.CONTENT_DIRECTORY );
        values.clear();
        //values.put(People.ContactMethods.KIND, Contacts.KIND_EMAIL);
        values.put(People.ContactMethods.DATA, "asd@hotmail.com");
        values.put(People.ContactMethods.TYPE, People.ContactMethods.TYPE_HOME);
        getContentResolver().insert(emailUri, values); 

    }
    public void populateContactList(){


        Cursor c = getContacts();
        String[] contacts = new String[]{
                ContactsContract.Data.DISPLAY_NAME,
                //ContactsContract.Data.CONTACT_STATUS,
                //ContactsContract.Contacts.STARRED,
                };
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.main,
                c, contacts, new int[] {R.id.ContactsLV});
        //ContactsLV.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contacts));
        ContactsLV.setAdapter(adapter);

    }
    private Cursor getContacts(){
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {
        //ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME,
        };
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
        //String selection = null;
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
        return managedQuery(uri, projection,selection,selectionArgs, sortOrder);
    }
}

Logcat Logcat

03-21 08:32:54.013: ERROR/InputDispatcher(64): Received spurious receive callback for unknown input channel.  fd=167, events=0x8
03-21 08:32:54.013: ERROR/InputDispatcher(64): Received spurious receive callback for unknown input channel.  fd=170, events=0x8
03-21 08:32:54.013: ERROR/InputDispatcher(64): Received spurious receive callback for unknown input channel.  fd=178, events=0x8
03-21 08:32:54.023: ERROR/InputDispatcher(64): Received spurious receive callback for unknown input channel.  fd=188, events=0x8
03-21 08:33:03.053: ERROR/TelephonyManager(64): Hidden constructor called more than once per process!
03-21 08:33:03.053: ERROR/TelephonyManager(64): Original: android, new: android
03-21 08:33:45.002: ERROR/libEGL(64): called unimplemented OpenGL ES API
03-21 08:33:45.002: ERROR/libEGL(64): called unimplemented OpenGL ES API
03-21 08:33:45.002: ERROR/libEGL(64): called unimplemented OpenGL ES API
03-21 08:33:45.002: ERROR/libEGL(64): called unimplemented OpenGL ES API
03-21 08:33:45.002: ERROR/libEGL(64): called unimplemented OpenGL ES API
03-21 08:33:45.011: ERROR/libEGL(64): called unimplemented OpenGL ES API
03-21 08:33:45.011: ERROR/libEGL(64): called unimplemented OpenGL ES API
03-21 08:33:45.021: ERROR/libEGL(64): called unimplemented OpenGL ES API

Double check above line 在行上方仔细检查

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.main,
            c, contacts, new int[] {R.id.ContactsLV});

with the SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to, int flags) SimpleCursorAdapter(上下文上下文,int布局,光标c,从int []到int标志的String [])

You just need to change the layout integer .just have a look at http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews 您只需要更改布局整数即可。只需看看http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews

The logcat segment that you have posted does not have a specific error stack trace in it. 您已发布的logcat段中没有特定的错误堆栈跟踪。 But following are the things you could check: 但是,您可以检查以下内容:

  1. Check that the permission is right. 检查权限是否正确。 You need to have Read contacts use permission. 您需要具有“读取联系人使用权限”。
  2. You will need the column ID selected in projection: ContactsContract.Contacts._ID 您将需要在投影中选择的列ID:ContactsContract.Contacts._ID
  3. As pointed by 100rabh, you need to look at the SimpleCursorAdapter. 正如100rabh所指出的,您需要查看SimpleCursorAdapter。 Specifically, the layout you pass should be the layout file containing the fields in which you want to populate the data and last integer array should correspond to the view id in which each of the column data has to be populated. 具体来说,您传递的布局应该是包含要在其中填充数据的字段的布局文件,而最后一个整数数组应对应于其中必须填充每个列数据的视图ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM