我有一个只显示图像的ListView。我想要一个功能,如果我点击ListView中的任何图像,它应该打开一个弹出菜单,弹出菜单应该包含3-4个复选框,其中一些名称代表用户想要将该图像放入的文件夹。 / p>
我到处搜索,但找不到任何此类教程。我该如何实施呢?
答案 0 :(得分:3)
尝试这样的事情:
ListView listview = (ListView) findViewById(R.id.list_item);
final CharSequence[] items = {" Easy "," Medium "," Hard "," Very Hard "};
final ArrayList selectedItems=new ArrayList();
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(ActualActivity.this);
builder.setTitle("This is a title");
builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//Here you add or remove the items from the list selectedItems. That list will be the result of the user selection.
if (isChecked) {
selectedItems.add(which);
} else if (selectedItems.contains(which)) {
selectedItems.remove(Integer.valueOf(which));
}
}
});
builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Do something when the user closes the dialog by pressing the Done button
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//Do something else if you want
}
});
builder.create();
builder.show();
}
});
基于this answer!