嗨我使用以下代码构建了一个对话框,可以从我的通话记录中选择一个联系人,但它只显示每个项目中的电话号码.如何更改它以在每个项目中显示“联系人姓名”“电话号码”?
我试过但似乎builder.setCursor()方法只允许我添加一列,我怎么能添加多个列?谢谢!
public class MainActivity extends Activity { @OverrIDepublic 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(); } });}
}
解决方法:
这是我的解决方案.您可以修改和修改它.
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();}
总结 以上是内存溢出为你收集整理的android – 当使用alertdialog builder的setCursor方法显示数据库中的项列表时,如何添加多个列?全部内容,希望文章能够帮你解决android – 当使用alertdialog builder的setCursor方法显示数据库中的项列表时,如何添加多个列?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)