问题描述
|
我正在尝试实现一个自定义光标适配器以显示联系人姓名和电话号码。但是我只收到姓名和电话号码为null的消息。请帮我弄清楚为什么我将电话号码作为null的原因。
<?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\"
/>
private AutoCompleteTextView destination;
private ContentResolver mContentResolver;
private static final String[] PEOPLE_PROJECTION = new String[] {
People._ID,People.NAME,People.NUMBER
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_view);
mContentResolver = getContentResolver();
Cursor cursor = managedQuery(
People.CONTENT_URI,PEOPLE_PROJECTION,null,null);
if(cursor == null ){
Log.i(getClass().getSimpleName(),\"cursor null\");
}
Calllistadapter adapter = new Calllistadapter(this,cursor);
destination = (AutoCompleteTextView) findViewById(R.id.autotextContacts);
destination.setAdapter(adapter);
}
public static class Calllistadapter extends CursorAdapter implements
Filterable {
Context context;
private ContentResolver mContent;
public Calllistadapter(Context context,Cursor c) {
super(context,c,true);
mContent = context.getContentResolver();
}
@Override
public View newView(Context context,Cursor cursor,ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
final View view = (View) inflater.inflate(R.layout.contacts_view,parent,false);
TextView tx = (TextView) view.findViewById(R.id.namelabel);
tx.setText(cursor.getString(cursor.getColumnIndex(People.NAME)));
return view;
}
@Override
public void bindView(View view,Context context,Cursor cursor) {
TextView tx = (TextView) view.findViewById(R.id.rishi_label);
tx.setText(cursor.getString(cursor.getColumnIndex(People.NAME)));
Log.i(\"LOG\",\"Contact NAME\" + cursor.getString(cursor.getColumnIndex(People.NAME)));
int numbercolumn = cursor.getColumnIndex(People.NUMBER);
String number;
number = cursor.getString(numbercolumn);
Log.i(\"LOG\",\"Contact NUMBER\" + number);
}
@Override
public String convertToString(Cursor cursor) {
return cursor.getString(cursor.getColumnIndex(People.NAME));
}
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (getFilterQueryProvider() != null) {
return getFilterQueryProvider().runQuery(constraint);
}
StringBuilder buffer = null;
String[] args = null;
if (constraint != null) {
buffer = new StringBuilder();
buffer.append(\"UPPER(\");
buffer.append(Contacts.People.NAME);
buffer.append(\") GLOB ?\");
args = new String[] { constraint.toString().toupperCase() + \"*\" };
}
return mContent.query(Contacts.People.CONTENT_URI,buffer == null ? null : buffer
.toString(),args,null);
}
}
谢谢
解决方法
我认为在下面的代码中您会得到答案。
if (Boolean.parseBoolean(hasPhone)) {
// Find all phone numbers
Cursor phones1 = getContentResolver().query(Phone.CONTENT_URI,null,Phone.CONTACT_ID + \" = \" + contactId,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.ContactsContract;
导入android.provider.ContactsContract.CommonDataKinds.Phone;
导入android.view.LayoutInflater;
导入android.view.View;
导入android.view.ViewGroup;
导入android.widget.CursorAdapter;
导入android.widget.Filterable;
导入android.widget.LinearLayout;
导入android.widget.TextView;
公共类Contactmodel扩展了CursorAdapter实现Filterable {
私有最终上下文上下文;
公共Contactmodel(上下文上下文){
super(context,null);
this.context =上下文;
}
@Override
public View newView(Context context,Cursor c,ViewGroup parent){
字符串编号= c.getString(c.getColumnIndex(Phone.NUMBER));
字符串名称= c.getString(c.getColumnIndex(Phone.DISPLAY_NAME));
最终LayoutInflater充气器= LayoutInflater.from(context);
//最终的TextView视图=(TextView)inflater.inflate(
// android.R.layout.simple_dropdown_item_1line,parent,false);
最终的LinearLayout l =(LinearLayout)inflater.inflate(
R.layout.contact_item,父级,false);
TextView视图=(TextView)l.getChildAt(0);
view.setText(name);
view =(TextView)l.getChildAt(1);
view.setText(number);
返回l;
}
@Override
public void bindView(View view,Context context,Cursor c){
字符串编号= c.getString(c.getColumnIndex(Phone.NUMBER));
字符串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(number);
}
@Override
public String convertToString(Cursor c){
返回c.getString(c.getColumnIndex(Phone.DISPLAY_NAME));
}
@Override
公共游标runQueryOnBackgroundThread(CharSequence约束){
如果(getFilterQueryProvider()!= null){
返回getFilterQueryProvider()。runQuery(constraint);
}
System.out.println(\“调试数据库中的搜索\”);
StringBuilder buffer = null;
String [] args = null;
if(constraint!= null){
缓冲区=新的StringBuilder();
buffer.append(\“ UPPER(\”);
buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
buffer.append(\“)GLOB?\”);
args = new String [] {约束.toString()。toUpperCase()+ \“ * \”};
}
返回context.getContentResolver()。query(Phone.CONTENT_URI,
PEOPLE_PROJECTION,缓冲区== null? null:buffer.toString(),
args,null);
}
私有静态最终String [] PEOPLE_PROJECTION =新String [] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,电话:NUMBER};
}