How to use setMultiChoiceItems() with a Custom AlertDialog that uses an efficiency arrayadapter?

StackOverflow https://stackoverflow.com/questions/3410810

  •  25-09-2019
  •  | 
  •  

Question

I am writing a music player that uses a custom Adapter extending BaseAdapter (efficiency adapter) that I want to display in an AlertDialog using setAdapter() where the user can either click on one of the songs to switch to that position in the playlist OR check songs to remove from the playlist. I tried using a custom click listener so that a user could just long click to remove the item from the list but the listview just doesn't work right... it was removing the wrong items (the ones at the end) even though the ArrayList contained the correct playlist items... (when I removed the item from the ArrayList, I passed it to the adapter which called notifyDataSetChanged... but that just didn't work as I mentioned. There is definitely a bug in the AlertDialog ListView... because there is no reason for it to have popped off the results from the end rather than the correct item.

So... the next method I would like to try is to use the setMultiChoiceItems() method of the AlertDialog... but it appears that it doesn't work with a custom adapter... only simple arrays. Will I have to subclass AlertDialog and Override the setMultiChoiceItems() method or is there a way I can make it work with an ArrayAdapter?

Basically, I can't figure out how to even iterate the list that the AlertDialog creates or whether it even passes that view somehow. In addition, I don't think I can even listen to clicks on checkboxes if I add those to the row. Any help will be greatly appreciated.

EDIT: Asking questions here is like magic... I answered my own question... this is how I did it. I added a hint to each checkbox which is the position of the item in the ArrayList. Then I used OnCheckedChangeListener to capture the selections. When you set a hint it adds text to the checkbox... since the background of the AlertDialog is white (even for clicked items?) I just set the hint text color to transparent.

holder.check.setHintTextColor(Color.TRANSPARENT);
holder.check.setHint(String.valueOf(position));
holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        int position = Integer.parseInt((String) buttonView.getHint());
        Log.v("onCheckedChanged", "Checked: "+isChecked+" returned: "+position+" which should be "+getItem(position).name);                         
    }

});
Was it helpful?

Solution

Refer This and This

then Pass a reference to byte[] in setMultiChoiceItems().

final boolean[] booleans = {false, true, false, true, false, false, false};

Then check the value of booleans inside setPositiveButton().

If you need to pass this AlertDialog around, then extend AlertDialog and have create a field boolean as described in 1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top