我想在单击ListView项目(使用自定义适配器)时创建一个对话框,但我真的不知道如何从ListView中获取一个特定的东西放入对话框。我想在对话框中显示说明。提前谢谢!
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder (WorkoutActivity.this);
alertDialog.setTitle("Description");
String selectedItem = (String) parent.getItemAtPosition(position);
alertDialog.setMessage(selectedItem);
alertDialog.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
alertDialog.show();
}
});
这是自定义适配器
class CustomAdapter extends BaseAdapter {
@Override
public int getCount() {
return IMAGES.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.customlayout, null);
ImageView image=(ImageView)view.findViewById(R.id.image);
TextView name=(TextView)view.findViewById(R.id.name);
TextView description=(TextView)view.findViewById(R.id.description);
image.setImageResource(IMAGES[i]);
name.setText(NAMES[i]);
description.setText(DESCRIPTIONS[i]);
return view;
}
}
答案 0 :(得分:0)
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
buider.setTitle("Description");
builder.setMessage(adapter.getItem(position).selectedItem);
builder.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
这将在diaog中显示selectedItem。
答案 1 :(得分:0)
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
....
//setTag in itemview
view.setTag(DESCRIPTIONS[i]);
return view;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
...
String description = (String) view.getTag()
//display description
}