Reputation: 129
Hi I used the following code to build a dialog box that is able to pick a contact from my call history, but it only displays the phone number in every item. How can i change it to display "Contact Name" + "Phone Number" in every item?
I tried but it seems the builder.setCursor() method only allows me to add one column, how can i add more than one column? Thank you!
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnPick = (Button) findViewById(R.id.btnPick);
btnPick.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String[] strFields = { android.provider.CallLog.Calls._ID,
android.provider.CallLog.Calls.NUMBER,
android.provider.CallLog.Calls.CACHED_NAME, };
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
final Cursor cursorCall = getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, strFields,
null, null, strOrder);
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle("Select recent contact");
android.content.DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface,
int item) {
cursorCall.moveToPosition(item);
Toast.makeText(
MainActivity.this,
cursorCall.getString(cursorCall
.getColumnIndex(android.provider.CallLog.Calls.NUMBER)),
Toast.LENGTH_LONG).show();
cursorCall.close();
return;
}
};
builder.setCursor(cursorCall, listener,
android.provider.CallLog.Calls.NUMBER);
builder.create().show();
}
});
}
}
Upvotes: 4
Views: 1015
Reputation: 1948
This is my solution. You may modify and adapt it.
public void getCallLog() {
String[] callLogFields = { CallLog.Calls._ID,
CallLog.Calls.NUMBER,
CallLog.Calls.CACHED_NAME,
CallLog.Calls.DATE};
String ORDER = CallLog.Calls.DATE + " DESC";
String WHERE = CallLog.Calls._ID + " IN (SELECT " + CallLog.Calls._ID + " FROM calls GROUP BY " + CallLog.Calls.NUMBER + ")";
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, callLogFields, WHERE, null, ORDER);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
if(cursor == null || !cursor.moveToFirst()) return;
final List<Map<String, String>> data = new ArrayList<>();
do
{
long time = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm", Locale.getDefault());
Date resultdate = new Date(time);
String date = sdf.format(resultdate);
String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
Map<String, String> map = new HashMap<>(4);
map.put("number", number);
map.put("name", name);
map.put("visible_name", name == null ? number : name);
map.put("date", date);
data.add(map);
} while (cursor.moveToNext());
if(!cursor.isClosed()) cursor.close();
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int item) {
String number = data.get(item).get("number");
Log.v("dva.re.number", number);
}
};
SimpleAdapter adapter = new SimpleAdapter(this, data,
android.R.layout.simple_list_item_2,
new String[] {"visible_name", "date"},
new int[] {android.R.id.text1,
android.R.id.text2,
});
dialogBuilder.setAdapter(adapter, listener);
dialogBuilder.setTitle("Choose from Call Log");
dialogBuilder.create().show();
}
Upvotes: 3
Reputation: 18921
It's not a quick solution, but one method that will work is to create your own content resolver that, in turn, calls the contacts content resolver.
Your content resolver should use an instance SQLiteQueryBuilder
object which you can call setProjectionMap()
on to effectively concatenate multiple column data from the contacts database into a single column in your cursor that you can then supply to your builder.setCursor()
method.
Upvotes: 0