0

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

Gishantha Darshana
  • 163
  • 1
  • 6
  • 17
  • Your list view should be in context of you custom dialog box. Try this – AndroidHacker Dec 05 '13 at 06:04
  • @AndroidHacker can you explain it more further please ? – Gishantha Darshana Dec 05 '13 at 06:06
  • Check out my post here http://stackoverflow.com/questions/20371218/how-to-set-a-custom-list-view-into-a-dialog-box/20371447#20371447 – AndroidHacker Dec 05 '13 at 06:07
  • Share your "dialog_list.xml". do you have any buttons on that xml file. If so, It will capture the click event. – Rethinavel Dec 05 '13 at 06:32
  • no there is no buttons inside it...just image view and some text views... – Gishantha Darshana Dec 05 '13 at 06:58
  • Your list view code is correct. There's no problem in that. Just check out my post – AndroidHacker Dec 05 '13 at 06:59
  • i have refered your post.. my problem is i have an arrayList like this [{Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}, {Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}, {Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}, {Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}, {Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}, {Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}] in my dialog box i just want to show these items.. that is why i use and adapter...what should i do.. ? – Gishantha Darshana Dec 05 '13 at 07:22
  • Code defined in my post fulfills all your requirement. For that certainly need an adapter and all other stuff required for creating custom dialog with custom list view – AndroidHacker Dec 05 '13 at 07:25

2 Answers2

1

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.

Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • i have done it...but still cant catch the click event in the list view :( – Gishantha Darshana Dec 05 '13 at 06:15
  • but i have to load the data thru the JSON..i have get the data from my service and holding by a ArrayList...just want to handle the click event... still it doesn't works.... :( – Gishantha Darshana Dec 05 '13 at 06:23
  • Are data appear in listview ?? – Niranj Patel Dec 05 '13 at 06:42
  • yes...successfully... i just need to handle the onclick event on the list :( – Gishantha Darshana Dec 05 '13 at 06:53
  • my arraylist like this [{Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}, {Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}, {Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}, {Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}, {Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}, {Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}] how can i convert this to char sequence ? – Gishantha Darshana Dec 05 '13 at 07:14
  • set **android:focusable="true"** on all item of view in listview – Niranj Patel Dec 05 '13 at 07:16
0

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();
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
  • my arraylist like this [{Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}, {Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}, {Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}, {Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}, {Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}, {Date=11/18/13, EndTime=09:00 AM, StartTime=08:00 AM}] how can i convert this to char sequence ? – Gishantha Darshana Dec 05 '13 at 06:38
  • please help me some way :( – Gishantha Darshana Dec 05 '13 at 08:10