1

I'm trying to do AlertDialog with custom list (image + text). I'm using custom adapter, it works, but I don't understand why the onClick event does not work. I had already tried create ListView, set my adapter to it and than set view to my AlertDialog, but I still can not catch the event. What am I doing wrong?

ArrayList<ItemData> list = ItemData.createFromMaterialArray(materials);
        MaterialsAdapter adapter = new MaterialsAdapter(this,
                R.layout.custom_material_item, R.id.text, list);


        AlertDialog.Builder materialTypesDialog = new AlertDialog.Builder(this);
        materialTypesDialog.setTitle(R.string.material);
        materialTypesDialog.setAdapter(adapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Nothing happens, why???
                Toast.makeText(context, "WORK", Toast.LENGTH_LONG).show();
            }
        });

        materialTypesDialog.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // Same problem
                Toast.makeText(context, "WORK", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        materialTypesDialog.show();

My adapter, i used similar adapter for spinners and it works fine.

class MaterialsAdapter extends ArrayAdapter<ItemData> {

private int groupId;

Activity context;
ArrayList<ItemData> list;
private LayoutInflater inflater;

/**
 * @param context
 * @param _groupId
 * @param _id
 * @param list
 */
MaterialsAdapter(Activity context, int _groupId, int _id, ArrayList<ItemData>
        list) {
    super(context, _id, list);
    this.list = list;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.groupId = _groupId;
}

/**
 * @param position
 * @param convertView
 * @param parent
 * @return
 */
public View getView(int position, View convertView, ViewGroup parent) {

    View itemView = inflater.inflate(groupId, parent, false);
    ImageView imageView = (ImageView) itemView.findViewById(R.id.image);

    imageView.setImageResource(list.get(position).getImageId());
    TextView textView = (TextView) itemView.findViewById(R.id.text);
    textView.setText(list.get(position).getTextId());

    return itemView;

}

/**
 * @param position
 * @param convertView
 * @param parent
 * @return
 */
public View getDropDownView(int position, View convertView, ViewGroup
        parent) {
    return getView(position, convertView, parent);

}
}

ItemData class

class ItemData {

private String id;
private int textId;
private int imageId;

/**
 * @param id
 * @param textId
 * @param imageId
 */
private ItemData(String id, int textId, int imageId) {
    this.id = id;
    this.textId = textId;
    this.imageId = imageId;
}

/**
 * @return
 */
int getTextId() {
    return textId;
}

/**
 * @return
 */
int getImageId() {
    return imageId;
}

/**
 * @return
 */
String getId() {
    return id;
}


/**
 * @return
 */
static ArrayList<ItemData> createFromMaterialArray(Material[] materials) {
    ArrayList<ItemData> itemDataList = new ArrayList<>();

    for (Material material : materials) {

        itemDataList.add(
                new ItemData(material.getName(),
                        material.textID,
                        material.imageID);
    }

    return itemDataList;
}}

Layout custom_material_item looks like simple LineralLayout with two items(TextView and ImageView).

negatiffx
  • 251
  • 1
  • 3
  • 9
  • http://stackoverflow.com/questions/15762905/how-can-i-display-a-list-view-in-an-android-alert-dialog this should help. – Chester Cobus Mar 25 '17 at 12:10
  • Thanks for the answer, it works when i use ArrayAdapter, but when i use my custom adapter OnClickListener does not work. The problem is not so much in the error as in the fact that I can not understand how to debug it. I can not understand what kind of events triggered when i click on the item in list. – negatiffx Mar 25 '17 at 12:38
  • 1
    remove materialTypesDialog.setOnItemSelectedListener event and add this AlertDialog alert = materialTypesDialog.create(); alert.show(); you might be creating the dialog incorrectly : http://stackoverflow.com/questions/7811117/custom-objects-in-alertdialog-list-how-to-get-a-display-string-and-then-the-act – Chester Cobus Mar 25 '17 at 12:48

2 Answers2

4

The problem was in my custom item Layout. In my parent LineralLayout was added Clickable="true", thats why OnClickListner didn't triggered.

negatiffx
  • 251
  • 1
  • 3
  • 9
1

Try to get ListView from your alertdialog and setOnItemClickListener like below:

materialTypesDialog.getListView().setOnItemClickListener(
new AdapterView.OnItemClickListener() {
   public void onItemClick(AdapterView<?> parent, View view, int position, 
    long id) {

        }
     });

Or Need to add below code to create and show alertDialog:

AlertDialog mAlertDialog = materialTypesDialog.create(); 
mAlertDialog.show();

And remove setOnItemSelectedListener. It is not working with AlertDialog.

pRaNaY
  • 24,642
  • 24
  • 96
  • 146