gpt4 book ai didi

android - 使用 alertdialog 构建器的 setCursor 方法显示数据库中的项目列表时如何添加多个列?

转载 作者:太空宇宙 更新时间:2023-11-03 10:19:33 25 4
gpt4 key购买 nike

您好,我使用以下代码构建了一个对话框,可以从我的通话记录中选择一个联系人,但它只显示每个项目中的电话号码。我如何更改它以在每个项目中显示“联系人姓名”+“电话号码”?

我试过了,但 builder.setCursor() 方法似乎只允许我添加一列,我如何添加多列?谢谢!

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();
}
});
}

最佳答案

这是我的解决方案。您可以对其进行修改和改编。

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 构建器的 setCursor 方法显示数据库中的项目列表时如何添加多个列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26522237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com