Reputation: 3133
I am trying to put Name and phone numbers from contacts to my customized listView with help of Custom CursorAdapter, But I am getting the following error
Exception
11-07 17:53:39.619: ERROR/AndroidRuntime(628): FATAL EXCEPTION: main
11-07 17:53:39.619: ERROR/AndroidRuntime(628): java.lang.IllegalArgumentException: column 'data1' does not exist
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:99)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at org.com.apis.ContactListCustomCursorAdapter.bindView(ContactListCustomCursorAdapter.java:37)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.widget.ListView.onMeasure(ListView.java:1109)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.view.View.measure(View.java:8171)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:563)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:378)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.view.View.measure(View.java:8171)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.view.View.measure(View.java:8171)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.view.View.measure(View.java:8171)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.os.Looper.loop(Looper.java:123)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at java.lang.reflect.Method.invoke(Method.java:521)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-07 17:53:39.619: ERROR/AndroidRuntime(628): at dalvik.system.NativeStart.main(Native Method)
Here is the Code I am using
Main Activity Code
public class ManipulationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_list_layout);
Cursor cursor = getCursor();
ListView contactList = (ListView)findViewById(R.id.contactList);
contactList.setAdapter(new ContactListCustomCursorAdapter(this, cursor)); //ContactListCustomCursorAdapter is a customized CursorAdapter. I have put the code below this class
}
//Get Cursor pointing Contacts
private Cursor getCursor() {
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Boolean.parseBoolean(hasPhone)) {
// You know it has a number so now query it like this
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
}
cursor.close();
return cursor;
}
}
ContactListCustomCursorAdapter Code
public class ContactListCustomCursorAdapter extends CursorAdapter {
private Context context;
private Cursor cursor;
private LayoutInflater inflater;
private int username;
private int phoneNumber;
public ContactListCustomCursorAdapter(Context context, Cursor c) {
super(context, c);
this.context = context;
this.cursor = c;
inflater = LayoutInflater.from(context);
username = cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView userName = (TextView)view.findViewById(R.id.userName);
userName.setText(cursor.getString(username));
TextView phoneNumber = (TextView)view.findViewById(R.id.userNumber);
phoneNumber.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = inflater.inflate(R.layout.custom_contact_list_item, parent, false);
return view;
}
}
I don't want to use the Contact Picker.
Upvotes: 1
Views: 3306
Reputation: 1
val contactData=data!!.data val c= contentResolver.query(contactData!!,null,null,null,null)
if (c!!.moveToFirst()){
val id= c!!.getString(c!!.getColumnIndexOrThrow(ContactsContract.Contacts._ID))
val hasPhone= c!!.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER))
if (hasPhone.equals("1")){
//list of phones
val phones= contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null
, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id, null,null)
//we will take one phone phones!!.moveToFirst() val phoneNumber = phones.getString(phones.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)) val name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME))
Upvotes: 0
Reputation: 3133
I have resolved the problem by making it sure that the database is opened when it is needed and is closed when not.
Upvotes: 0
Reputation: 21
I also got this error, after spending an couple of hours i got the answer. There are CONSTANT COLUMN MISMATCH problem, the 'data1' or NUMBER is a constant belongs to Phone class.
The ContactsContract.Contacts.CONTENT_URI does not provide number, for that you have to mention the projection with query it will give you the result, with help of this you can get ._id, display_name. But, if you want to get number from id then you have to something like this,
Cursor cursorPhone = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.Contacts._ID +" = " + item_ID,
null,
null);
Thank you
Upvotes: 2
Reputation: 972
First of all the cursor you are passing to the CursorAdapter is closed. If you are assigning a cursor to an adapter you should not close it, just call changeCursor(null) in onDestroy for your activity.
Secondly, from the code I assume you want to show a list of all Contacts and their Phone numbers, so you getCursor method should be something like this
private Cursor getCursor() {
Cursor cursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
return cursor;
}
And your bindView should be like
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView userName = (TextView)view.findViewById(R.id.userName);
userName.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
TextView phoneNumber = (TextView)view.findViewById(R.id.userNumber);
phoneNumber.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
Upvotes: 0