【问题标题】:android developer alert dialog + listview functionandroid 开发者警告对话框 + listview 功能
【发布时间】:2014-03-02 14:14:18
【问题描述】:

我写了一些代码来实现alertDialog中的listView。现在,当我想在列表视图上实现某些功能时,我没有成功。这是他 alertDialog 的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Name");
ListView modeList = new ListView(this);
String[] stringArray = new String[] {"red" , "green" , "blue"};
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this,         android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);
builder.setView(modeList);
final Dialog dialog = builder.create();
dialog.show();

如何在点击的项目上实现功能?

【问题讨论】:

  • 您必须创建自己的自定义适配器并为行中确定的项目添加侦听器。

标签: java android eclipse android-listview


【解决方案1】:

您可以在警报对话框中尝试列表的build in implementation。这取决于用例。如果您只想使用此对话框从列表中选择值(没有自定义列表项 UI) - 这是一个好方法。

【讨论】:

    【解决方案2】:

    您应该能够执行以下代码:它显示一个带有包含您的值的 listView 的 alertDialog:

    CharSequence[]  stringArray =  {"red" , "green" , "blue"};
    
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Select Name");
    builder.setItems(stringArray, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
             // Do something with the selection
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 2020-09-24
      相关资源
      最近更新 更多