ListView items does not respond in a custom dialog box

2k views Asked by At

I am developing an app which is displaying some data to a ListView. The data is perfectly shown in the ListView (custom list view). i have used a custom adapter also which is extended by a BaseAdapter. i have modified my app to pop up a custom dialog box when some duplicated records are there in the list view. so my problem is the items in the custom dialog (in the list view ) does not respond to the onclick listener

here is my code (which is inside the adapter class)

        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();
    }

can someone tell me where the problem is ?

i have refer the developer instructions also.. they stated that below code should work.. but it is not responding at all.. no errors..no exceptions..but doesn't works

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();
            }
        })

please help me

2

There are 2 answers

6
Niranj Patel On

If you are display custom layout including ListView then no need setAdapter on AlertDialog.

Just setAdapter on ListView Instead of 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();
    }

________________________________

You can also use default functionality instead of custom view..

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.

2
NagarjunaReddy On

Try like this

 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();