gpt4 book ai didi

android - 在 alertDialog android 中显示 ListView

转载 作者:行者123 更新时间:2023-11-29 15:38:41 25 4
gpt4 key购买 nike

当用户单击列表项时,将显示一个对话框,其中包含“查看”、“删除”或“更新”等选项 ListView 项。第一次单击 ListView 项会显示带有选项的警报对话框,但是当我第二次单击另一个 ListView 项或同一个 ListView 项时,应用程序崩溃。提前致谢。

错误:java.lang.IllegalStateException:指定的子项已有父项。您必须先对 child 的 parent 调用 removeView()。

  //The main list displayed when the user log in successfully  
ListView = (ListView) findViewById(R.id.list);
ListView2 = new ListView(this); //List to be displayed in the alert Dailog box
ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long i = ListView.getItemIdAtPosition(position);
showAlertDailog();//The dialog box method
}
});
if(mCursorAdapter == null) //To check if the list is empty
{
TextView emptyTextView = (TextView) findViewById(R.id.empty);
emptyTextView.setText("No Notes");
ListView.setEmptyView(emptyTextView);
}else {
mCursorAdapter = new DataCursorAdapter(this, null);
ListView.setAdapter(mCursorAdapter);
}
public void showAlertDailog()
{
String [] items = {"View","Delete","Update"};

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_todo_2,R.id.textView4,items);
ListView2.setAdapter(adapter);


final android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(HomeActivity.this);
builder.setView(ListView2)
.setCancelable(false)
.setPositiveButton("Close",null);
android.app.AlertDialog alertDialog = builder.create();
alertDialog.show();
}

最佳答案

ArrayList<String> arrayList = new ArrayList<String>();

CharSequence[] animals = arrayList.toArray(new String[arrayList.size()]);

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

dialogBuilder.setItems(animals, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {

String selectedText = animals[item].toString();
}
});

AlertDialog alertDialogObject = dialogBuilder.create();

alertDialogObject.show();

This is simple way to display list inside alert dialog, where in arraylist just add string whatever you have to display inside alert dialog list.

关于android - 在 alertDialog android 中显示 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45188345/

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