问题描述
- 请教大家,希望读取出来的联系人信息(名字和电话号码)能够在ListView 中 显示
-
将读取的联系人信息在ListView中显示,但是不知道代码哪里出错了,始终无法显示,请大家指教,谢谢
activity_mainxmlns: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)
解决方案
http://www.cnblogs.com/sunjialiang/archive/2011/08/17/2142794.html
解决方案二:
参考:
http://blog.csdn.net/snwrking/article/details/7601794
解决方案三:
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
ArrayAdapter<String> adapter;
List<String> data = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
readData();// 放在这里
adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
}
public void readData() {
Cursor cursor = null;
try {
cursor = getContentResolver().query(Contacts.CONTENT_URI, null,
null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String displayName = cursor.getString(cursor
.getColumnIndex(Contacts.DISPLAY_NAME));
String contactId = cursor.getString(cursor
.getColumnIndex(Contacts._ID));
Cursor phones = getContentResolver().query(
Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + contactId, null, null);
phones.moveToNext();
String phoneNumber = phones.getString(phones
.getColumnIndex(Phone.NUMBER));
data.add(displayName+"n"+phoneNumber);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}