Android 无法在自定义联系人光标适配器中获取电话号码
我正在尝试实现一个自定义游标适配器来显示联系人的姓名和号码。但我得到的唯一姓名和号码是空的。请帮助我找出为什么我得到的号码是空的Android 无法在自定义联系人光标适配器中获取电话号码,android,Android,我正在尝试实现一个自定义游标适配器来显示联系人的姓名和号码。但我得到的唯一姓名和号码是空的。请帮助我找出为什么我得到的号码是空的 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fil
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<AutoCompleteTextView android:id="@+id/autotextContacts"
android:completionThreshold="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/namelabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
/>
<TextView
android:id="@+id/numberlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:layout_below = "@id/namelabel"
/>
谢谢我想在下面的代码中你会得到答案
if (Boolean.parseBoolean(hasPhone)) {
// Find all phone numbers
Cursor phones1 = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones1.moveToNext()) {
String number = phones1.getString(phones1
.getColumnIndex(Phone.NUMBER));
int type0 = phones1.getInt(phones1
.getColumnIndex(Phone.TYPE));
switch (type0) {
case Phone.TYPE_HOME:
// do something with the Home number here...
card.Homeno = number;
break;
case Phone.TYPE_MOBILE:
// do something with the Mobile number here...
card.MobileNo = number;
break;
case Phone.TYPE_WORK:
// do something with the Work number here...
card.WorkNo = number;
break;
case Phone.TYPE_OTHER:
// do something with the Other number here...
allphones += "\nOther No. : " + number;
break;
case Phone.TYPE_CUSTOM:
// do something with the Other number here...
card.CustomNo=number;
break;
}
}
phones1.close();
}
包my.package.androidapp.model;
导入my.package.androidapp.R;
导入android.content.ContentResolver;
导入android.content.Context;
导入android.database.Cursor;
导入android.provider.Contacts;
导入android.provider.contacts合同;
导入android.provider.contacts contract.CommonDataTypes.Phone;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.CursorAdapter;
导入android.widget.Filterable;
导入android.widget.LinearLayout;
导入android.widget.TextView;
公共类Contactmodel扩展CursorAdapter实现可过滤{
私人最终语境;
公共联系人模型(上下文){
super(上下文,null);
this.context=上下文;
}
@凌驾
公共视图newView(上下文上下文、光标c、视图组父对象){
String number=c.getString(c.getColumnIndex(Phone.number));
String name=c.getString(c.getColumnIndex(Phone.DISPLAY_name));
最终LayoutFlater充气机=LayoutFlater.from(上下文);
//最终文本视图=(文本视图)充气器。充气(
//android.R.layout.simple\u下拉列表\u项目\u 1行,父项,false);
最终线性布局l=(线性布局)充气器。充气(
R.layout.contact_项目,父项,假);
TextView视图=(TextView)l.getChildAt(0);
view.setText(名称);
视图=(TextView)l.getChildAt(1);
view.setText(数字);
返回l;
}
@凌驾
public void bindView(视图、上下文、光标c){
String number=c.getString(c.getColumnIndex(Phone.number));
String dName=c.getString(c.getColumnIndex(Phone.DISPLAY_NAME));
TextView nameV=(TextView)((LinearLayout)视图).getChildAt(0);
nameV.setText(dName);
TextView numV=(TextView)((LinearLayout)视图).getChildAt(1);
numV.setText(数字);
}
@凌驾
公共字符串convertToString(游标c){
返回c.getString(c.getColumnIndex(Phone.DISPLAY_NAME));
}
@凌驾
公共游标runQueryOnBackgroundThread(CharSequence约束){
if(getFilterQueryProvider()!=null){
返回getFilterQueryProvider().runQuery(约束);
}
System.out.println(“数据库中的调试搜索”);
StringBuilder缓冲区=null;
字符串[]args=null;
if(约束!=null){
缓冲区=新的StringBuilder();
buffer.append(“上限(”);
buffer.append(Contacts contract.Contacts.DISPLAY\u NAME);
buffer.append(“)GLOB?”);
args=新字符串[]{constraint.toString().toUpperCase()+“*”};
}
返回context.getContentResolver().query(Phone.CONTENT\u URI,
PEOPLE\u投影,buffer==null?null:buffer.toString(),
args,null);
}
私有静态最终字符串[]人\投影=新字符串[]{
联系人合同联系人。\u ID,
Contacts contract.Contacts.DISPLAY_NAME,Phone.NUMBER};
}
也许您应该看看contacts合同api,因为people api已被弃用。这可能是你的问题所在。Cou可以看一下这个教程(),它演示了contacts contract的使用,但是没有游标适配器,或者看一下api文档,谢谢,我根据您的回答发布了我的解决方案
if (Boolean.parseBoolean(hasPhone)) {
// Find all phone numbers
Cursor phones1 = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones1.moveToNext()) {
String number = phones1.getString(phones1
.getColumnIndex(Phone.NUMBER));
int type0 = phones1.getInt(phones1
.getColumnIndex(Phone.TYPE));
switch (type0) {
case Phone.TYPE_HOME:
// do something with the Home number here...
card.Homeno = number;
break;
case Phone.TYPE_MOBILE:
// do something with the Mobile number here...
card.MobileNo = number;
break;
case Phone.TYPE_WORK:
// do something with the Work number here...
card.WorkNo = number;
break;
case Phone.TYPE_OTHER:
// do something with the Other number here...
allphones += "\nOther No. : " + number;
break;
case Phone.TYPE_CUSTOM:
// do something with the Other number here...
card.CustomNo=number;
break;
}
}
phones1.close();
}
package my.package.androidapp.model;
import my.package.androidapp.R;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Contactmodel extends CursorAdapter implements Filterable {
private final Context context;
public Contactmodel(Context context) {
super(context, null);
this.context = context;
}
@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
String number = c.getString(c.getColumnIndex(Phone.NUMBER));
String name = c.getString(c.getColumnIndex(Phone.DISPLAY_NAME));
final LayoutInflater inflater = LayoutInflater.from(context);
// final TextView view = (TextView) inflater.inflate(
// android.R.layout.simple_dropdown_item_1line, parent, false);
final LinearLayout l = (LinearLayout) inflater.inflate(
R.layout.contact_item, parent, false);
TextView view = (TextView) l.getChildAt(0);
view.setText(name);
view = (TextView) l.getChildAt(1);
view.setText(number);
return l;
}
@Override
public void bindView(View view, Context context, Cursor c) {
String number = c.getString(c.getColumnIndex(Phone.NUMBER));
String dName = c.getString(c.getColumnIndex(Phone.DISPLAY_NAME));
TextView nameV = (TextView) ((LinearLayout) view).getChildAt(0);
nameV.setText(dName);
TextView numV = (TextView) ((LinearLayout) view).getChildAt(1);
numV.setText(number);
}
@Override
public String convertToString(Cursor c) {
return c.getString(c.getColumnIndex(Phone.DISPLAY_NAME));
}
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (getFilterQueryProvider() != null) {
return getFilterQueryProvider().runQuery(constraint);
}
System.out.println("debug search in the db");
StringBuilder buffer = null;
String[] args = null;
if (constraint != null) {
buffer = new StringBuilder();
buffer.append("UPPER(");
buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
buffer.append(") GLOB ?");
args = new String[] { constraint.toString().toUpperCase() + "*" };
}
return context.getContentResolver().query(Phone.CONTENT_URI,
PEOPLE_PROJECTION, buffer == null ? null : buffer.toString(),
args, null);
}
private static final String[] PEOPLE_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME, Phone.NUMBER };
}