繁体   English   中英

使用自定义适配器在AlertDialog中单击侦听器

[英]Click listener in AlertDialog with custom adapter

我正在尝试使用自定义列表(图像+文本)执行AlertDialog。 我正在使用自定义适配器,它可以工作,但是我不明白为什么onClick事件不起作用。 我已经尝试创建ListView,将其设置为适配器,然后将视图设置为AlertDialog,但仍然无法捕获该事件。 我究竟做错了什么?

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

我的适配器,我对旋转器使用了类似的适配器,并且工作正常。

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

布局custom_material_item看起来像带有两个项目(TextView和ImageView)的简单LineralLayout。

问题出在我的自定义项目布局中。 在我的父级LineralLayout中添加了Clickable =“ true”,这就是为什么未触发OnClickListner的原因。

尝试从您的alertdialog和setOnItemClickListener获取ListView,如下所示:

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

        }
     });

或需要添加以下代码来创建并显示alertDialog:

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

并删除setOnItemSelectedListener 它不能与AlertDialog一起使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM