qq_31478263
qq_31478263
2015-09-28 12:16

请教大家,希望读取出来的联系人信息(名字和电话号码)能够在ListView 中 显示

  • android
  • list

将读取的联系人信息在ListView中显示,但是不知道代码哪里出错了,始终无法显示,请大家指教,谢谢
activity_main

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.IWIN.providertext.MainActivity" >

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     />

MainActivity

package com.IWIN.providertext;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

ListView listView;
ArrayAdapter<String> adapter;
List<String> data= new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView=(ListView)findViewById(R.id.listView);

    adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data);
    listView.setAdapter(adapter);
    readData();
}
public void readData(){
    Cursor cursor=null;
    try{
         cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        if(cursor.moveToFirst()){
            do{
                String DisplayName=cursor.getString(cursor.getColumnIndex("ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME"));
                int number=cursor.getInt(cursor.getColumnIndex("ContactsContract.CommonDataKinds.Phone.NUMBER"));
                data.add(DisplayName+"\n"+number);
            }while(cursor.moveToNext());
        }
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        if(cursor!=null){
            cursor.close();
        }
    }
}

}

LogCat显示的是:
09-28 12:11:59.308: E/Cursor(675): requesting column name with table name -- ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
09-28 12:11:59.308: E/Cursor(675): java.lang.Exception
09-28 12:11:59.308: E/Cursor(675): at android.database.AbstractCursor.getColumnIndex(AbstractCursor.java:243)
09-28 12:11:59.308: E/Cursor(675): at com.IWIN.providertext.MainActivity.readData(MainActivity.java:35)
09-28 12:11:59.308: E/Cursor(675): at com.IWIN.providertext.MainActivity.onCreate(MainActivity.java:27)
09-28 12:11:59.308: E/Cursor(675): at android.app.Activity.performCreate(Activity.java:4466)
09-28 12:11:59.308: E/Cursor(675): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答