如何使用AlertDialog.Builder onItemClickListener创建ListView?

时间:2014-12-31 13:30:03

标签: java android listview alertdialog onitemclicklistener

我在使用ListView和onItemClickListener添加AlertDialog.Builder时遇到问题...怎么做?你能给出一个代码示例

2 个答案:

答案 0 :(得分:2)

试试这个

  private void AlertDialogView() {
    final CharSequence[] items = { "One", "Two", "Three", "Four" };

    AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);//ERROR ShowDialog cannot be resolved to a type
    builder.setTitle("Alert Dialog with ListView and Radio button");
    builder.setSingleChoiceItems(items, -1,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(), items[item],
                            Toast.LENGTH_SHORT).show();
                }
            });

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT)
                    .show();
        }
    });

    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT)
                    .show();
        }
    });

    AlertDialog alert = builder.create();
    alert.show();
}

答案 1 :(得分:-1)

考虑listView是您的ListView,MainActivity是您的活动名称。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        new AlertDialog.Builder(MainActivity.this)
                .setTitle("Title")
                .setMessage("Position clicked: " + position)
                .create()
                .show();
    }
});