gpt4 book ai didi

android - 将 onlongclick 监听器添加到警报对话框

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:00:19 29 4
gpt4 key购买 nike

我在 android 中有一个 AlertDialog,其中包含来自 sqlite 的好友列表。当我单击列表中的好友名称时,会调用该好友。我想要做的是在列表中也添加一个 longclicklistener,以便系统提示我删除列表中的好友。我无法让 onlclick 和 onlongclick 处理同一元素。有人可以在这里给我指点吗?我已经在 android 上工作了几个月。感谢您的帮助!

private void displayBuddyList(String region) {
final String region2 = region;
Context context = getApplicationContext();
dh = new DataBaseHelper(context);

List<String> bnames = dh.selectBuddies();
Log.d(TAG, "Buddy Names: " +bnames);



final CharSequence[] buds = bnames.toArray(new CharSequence[bnames.size()]);
// final CharSequence[] items = {"Mark", "Vikrant", "Olle,"Jane","Dan"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select a Buddy");
builder.setItems(buds, new DialogInterface.OnClickListener() {



public void onClick(DialogInterface dialogInterface, int item) {

// showShortToast("Clicked on:"+buddy[item]);
String ptcode = buds[item].toString();;




if (region2 == "A") {

callbuddy(ptcode,region2);

} else if (region2 == "E") {

callbuddy(ptcode,region2);


} else if (region2 == "P") {

callbuddy(ptcode,region2);



} else {
showShortToast("We have a bug");
}

return;
}
});
builder.create().show();
}

最佳答案

添加 OnLongClickListener 的一种方法是覆盖对话框的 OnShowListener 并在 onShow(DialogInterface dialog) 方法中设置 OnItemLongClickListener。试一试:

private void displayBuddyList(String region) {
final String region2 = region;
Context context = getApplicationContext();
dh = new DataBaseHelper(context);
List<String> bnames = dh.selectBuddies();
Log.d(TAG, "Buddy Names: " +bnames);

final CharSequence[] buds = bnames.toArray(new CharSequence[bnames.size()]);
// final CharSequence[] items = {"Mark", "Vikrant", "Olle,"Jane","Dan"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select a Buddy");
builder.setItems(buds, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialogInterface, int item) {
// showShortToast("Clicked on:"+buddy[item]);
String ptcode = buds[item].toString();;
if (region2 == "A") {
callbuddy(ptcode,region2);
} else if (region2 == "E") {
callbuddy(ptcode,region2);
} else if (region2 == "P") {
callbuddy(ptcode,region2);
} else {
showShortToast("We have a bug");
}
return;
}
});

final AlertDialog ad = builder.create(); //don't show dialog yet
ad.setOnShowListener(new OnShowListener()
{
@Override
public void onShow(DialogInterface dialog)
{
ListView lv = ad.getListView(); //this is a ListView with your "buds" in it
lv.setOnItemLongClickListener(new OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
Log.d("Long Click!","List Item #"+position+"was long clicked");
return true;
}
});
}
});
ad.show();

关于android - 将 onlongclick 监听器添加到警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9145628/

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