Not getting phone number in custom contact cursor adapter
i am trying to implement a custom cursor adapter to show contacts name and number.But i am getting only name and number is coming as nul开发者_如何学运维l.Please help me figure out why i am getting number as 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, 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,
PEOPLE_PROJECTION, buffer == null ? null : buffer
.toString(), args,
null);
}
}
Thanks
I think in below code you will get your answer.
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 };
}
加载中,请稍侯......
精彩评论