Reputation:
btn_add_from_contacts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
builder = new AlertDialog.Builder(getActivity());
View view = getLayoutInflater().inflate(R.layout.layout_add_from_contacts, null);
builder.setView(view);
/* ********************* */
phones = getActivity().getApplication().getContentResolver().query
(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
String name,phoneNumber;
while(phones.moveToNext()) {
name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.w("","Names: " +name+"\nPhone: "+phoneNumber);
alertDialog = builder.create();
alertDialog.show();
}
/* ********************* */
}
});
Here it is the code, all I want is to display my contacts from phone as here. I am just displaying it in while loop once but it is not giving me any output. Further I am using fragment. If any one knows what's wrong with code just help me out.
Here is the Logcat:
03-29 09:14:29.475 16754-16759/com.example.nomanikram.epilepsyseizuredetection I/zygote: Increasing code cache capacity to 1024KB
03-29 09:14:31.342 16754-16754/com.example.nomanikram.epilepsyseizuredetection W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed. 03-29 09:14:31.343 16754-16754/com.example.nomanikram.epilepsyseizuredetection W/ViewRootImpl[MainActivity]: Dropping event due to root view being removed: MotionEvent { action=ACTION_UP, actionButton=0, id[0]=0, x[0]=116.09668, y[0]=382.5547, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=10351594, downTime=10351588, deviceId=0, source=0x1002 } 03-29 09:14:31.343 16754-16754/com.example.nomanikram.epilepsyseizuredetection W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
Upvotes: 0
Views: 78
Reputation: 2171
You can do this to display contacts,
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Contacts List");
ArrayList<String> array=new ArrayList<String>();
phones = getActivity().getApplication().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
String name,phoneNumber;
while(phones.moveToNext()) {
name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.w("","Names: " +name+"\nPhone: "+phoneNumber);
array.add(name+"-->"+phoneNumber);
}
builder.setItems(array, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
//item clicked
}
});
builder.show();
Upvotes: 0
Reputation: 11
One possibility it to add all the contacts to your
R.layout.layout_add_from_contacts
(would be helpful if it is vertical linear layout). You can do that by inflating and filling separate view, like
R.layout.contact_item,
(which you need to create) for every contact, and adding it to the linear layout, which will be used as a view for the dialog window.
Other easier solution would be to use ArrayAdapter, like here: How can I display a list view in an Android Alert Dialog?
Upvotes: 0