ListView项目在自定义对话框中不响应

时间:2013-12-05 06:02:51

标签: android listview customdialog

我正在开发一个向ListView显示一些数据的应用程序。数据完美地显示在ListView(自定义列表视图)中。我也使用了一个由BaseAdapter扩展的自定义适配器。我修改了我的应用程序,当列表视图中有一些重复的记录时,弹出一个自定义对话框。所以我的问题是自定义对话框中的项目(在列表视图中)没有响应onclick监听器

这是我的代码(在适配器类中)

        public void showDuplicateDialog(ArrayList<HashMap<String, String>> list){

        AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(activity);
        LayoutInflater infl = activity.getLayoutInflater();
        View view = infl.inflate(R.layout.dialog_list, null);

        ListView lv = (ListView) view.findViewById(R.id.dialogList);

        //NewsRowAdapter nw = new NewsRowAdapter(mContext, activity, R.layout.dialog_row, list);

        SimpleAdapter sim = new SimpleAdapter(mContext, list, R.layout.dialog_row,  new String[] { STIME,END, DATE }, new int[] {
                R.id.stime2,R.id.etime2, R.id.blank2});

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(mContext, "item clicked ", Toast.LENGTH_LONG).show();
            }
        });

        /*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                  android.R.layout.two_line_list_item, android.R.id.text1, Names);*/

        alertDialogBuilder2.setView(view);
        alertDialogBuilder2.setAdapter(sim, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(mContext, "item clicked ", Toast.LENGTH_LONG).show();
            }
        })



        .setPositiveButton("Accept", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(mContext, "Accepted", Toast.LENGTH_LONG).show();
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });

        alertDialogBuilder2.show();
    }

有人可以告诉我问题在哪里吗?

我也参考了开发人员的说明..他们说下面的代码应该有效.. 但它根本没有响应..没有错误..没有例外..但是不起作用

alertDialogBuilder2.setAdapter(sim, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(mContext, "item clicked ", Toast.LENGTH_LONG).show();
            }
        })

请帮帮我

2 个答案:

答案 0 :(得分:1)

如果您显示包含ListView的自定义布局,则不需要在AlertDialog上设置setAdapter。

只需在ListView上设置适配器而不是AlertDialog。

public void showDuplicateDialog(ArrayList<HashMap<String, String>> list){

        AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(activity);
        LayoutInflater infl = activity.getLayoutInflater();
        View view = infl.inflate(R.layout.dialog_list, null);

        ListView lv = (ListView) view.findViewById(R.id.dialogList);

        //NewsRowAdapter nw = new NewsRowAdapter(mContext, activity, R.layout.dialog_row, list);

        SimpleAdapter sim = new SimpleAdapter(mContext, list, R.layout.dialog_row,  new String[] { STIME,END, DATE }, new int[] {
                R.id.stime2,R.id.etime2, R.id.blank2});

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(mContext, "item clicked ", Toast.LENGTH_LONG).show();
            }
        });

        /*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                  android.R.layout.two_line_list_item, android.R.id.text1, Names);*/

        alertDialogBuilder2.setView(view);
        lv.setAdapter(sim); // Set Adapter to listview




        alertDialogBuilder2.setPositiveButton("Accept", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(mContext, "Accepted", Toast.LENGTH_LONG).show();
                    }
                })


        alertDialogBuilder2.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });

        alertDialogBuilder2.show();
    }

<强> _ __ _ __ _ __ _ __ _ 的__ _ __ _ __ _ __ _ __ _ ____

您也可以使用默认功能而非自定义视图..

final CharSequence[] items = {"Foo", "Bar", "Baz"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Make your selection");
builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
         // Do something with the selection
    }
});
AlertDialog alert = builder.create();
alert.show();

check this for more about alertdilaog.

答案 1 :(得分:0)

试试这个

 final CharSequence[] items = { "Facebook", "Twitter", "Email" };
     AlertDialog.Builder builder = new AlertDialog.Builder(activity);
     builder.setTitle("Share");
     builder.setItems(items,    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog,    int item) { 



     if (items[item].equals("Facebook")) {  

     } else if (items[item].equals("Twitter")) {

     } else if (items[item].equals("Email")) {

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