我尝试使用自定义列表(图片+文字)进行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看起来像带有两个项目的简单LineralLayout(TextView和ImageView)。
答案 0 :(得分:3)
问题出在我的自定义项目布局中。在我的父LineralLayout中添加了Clickable =“true”,这就是为什么OnClickListner没有被触发。
答案 1 :(得分:1)
尝试从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
。