我是新来的android.When我试图让联系人姓名及其工作正常,但我想只得到的数字,但我不能这样做。我的code为: -
包com.example.sqllitecontactlist;
进口android.app.Activity;
进口android.database.Cursor;
进口android.os.Bundle;
进口android.provider.ContactsContract;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.ListView;
进口android.widget.SimpleCursorAdapter;
进口android.widget.TextView;
进口android.widget.AdapterView.OnItemClickListener;
公共类PhoneBookActivity延伸活动{
// Android的列表视图对象
ListView控件listViewPhoneBook;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.phone_book);
//从XML文件中的ListView参考
listViewPhoneBook =(ListView控件)findViewById(R.id.listPhoneBook);
的String [] arrayColumns =新的String [] {ContactsContract.CommonDataKinds.Phone.NUMBER};
// arrayViewID认为的id将映射到只有在这里textViewPhone
INT [] arrayViewID =新INT [] {R.id.textViewNumber};
光标光标= getContentResolver()查询(ContactsContract.Contacts.CONTENT_URI,arrayColumns,NULL,NULL,NULL);
SimpleCursorAdapter适配器=新SimpleCursorAdapter(这一点,R.layout.each_contact,光标,arrayColumns,arrayViewID);
listViewPhoneBook.setAdapter(适配器); }}
当我执行这个它说:java.lang.IllegalArgumentException:如果无效的列数据1。我用Google搜索了很多,并应用了一些解决方案,但失败了。请帮我出
解决方案 getNumber(this.getContentResolver());
公共无效getNumber(ContentResolver的CR)
{
光标手机= cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL,NULL,NULL,NULL);
//使用光标来访问触点
而(phones.moveToNext())
{
字符串名称= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
//获取显示名称
phoneNumber的= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//获取电话号码
的System.out.println(..................+ phoneNumber的);
}
}
activity_main.xml
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。>
<的ListView
机器人:layout_width =match_parent
机器人:layout_height =FILL_PARENT
机器人:ID =@ + ID / LV/>
< / RelativeLayout的>
MainActivity.java
公共类MainActivity延伸活动{
字符串phoneNumber的;
ListView的LV;
ArrayList的<字符串> AA =新的ArrayList<字符串>();
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
LV =(ListView控件)findViewById(R.id.lv);
getNumber(this.getContentResolver());
}
公共无效getNumber(ContentResolver的CR)
{
光标手机= cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL,NULL,NULL,NULL);
而(phones.moveToNext())
{
字符串名称= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber的= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
的System.out.println(..................+ phoneNumber的);
aa.add(phoneNumber的);
}
phones.close()//关闭游标
ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,
android.R.layout.simple_list_item_1,AA);
lv.setAdapter(适配器);
在列表//显示联系人号码
}
}
快照
请确保您有这方面的清单
<使用-权限的Android:名称=android.permission.READ_CONTACTS/>
I am new to android.When i try to get contact names its working fine but i want to get numbers only, but i am not able to do so. My code is:-
package com.example.sqllitecontactlist;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class PhoneBookActivity extends Activity {
//Android listview object
ListView listViewPhoneBook;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.phone_book);
//get the ListView Reference from xml file
listViewPhoneBook=(ListView)findViewById(R.id.listPhoneBook);
String[] arrayColumns = new String[]{ ContactsContract.CommonDataKinds.Phone.NUMBER};
// arrayViewID is the id of the view it will map to here textViewPhone only
int[] arrayViewID = new int[]{R.id.textViewNumber};
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, arrayColumns, null, null, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.each_contact, cursor, arrayColumns, arrayViewID);
listViewPhoneBook.setAdapter(adapter); }}
When i execute this it says "java.lang.IllegalArgumentException: Invalid column data1". i have googled it a lot and applied some solution but failed. Please help me out
解决方案 getNumber(this.getContentResolver());
public void getNumber(ContentResolver cr)
{
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
// use the cursor to access the contacts
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// get display name
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// get phone number
System.out.println(".................."+phoneNumber);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/lv"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
String phoneNumber;
ListView lv;
ArrayList <String> aa= new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView) findViewById(R.id.lv);
getNumber(this.getContentResolver());
}
public void getNumber(ContentResolver cr)
{
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println(".................."+phoneNumber);
aa.add(phoneNumber);
}
phones.close()// close cursor
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,aa);
lv.setAdapter(adapter);
//display contact numbers in the list
}
}
snap shot
Make sure you have this in manifest
<uses-permission android:name="android.permission.READ_CONTACTS"/>