我有一个ListView,它包含一定数量的Names,当我从ListView中单击一个项目时,我希望弹出一个ListDialog来显示数据库中的某些数据。那可能吗?
如果单击“列表对话框”中的某个项目后,如果“是”(如果可能),那么另一个列表对话框也可能会从中出现?像List Dialog嵌套?
非常感谢!
答案 0 :(得分:0)
是。只需要一个新的DialogFragment调用一些带有一些args的newInstance()来指定你想要的东西。
在您的列表活动中:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Cursor c = (Cursor) this.getListAdapter().getItem(position);
int index = c.getInt(c.getColumnIndexOrThrow(COLUMN_NAME));
DialogFragment newFragment = MyDialogFragment.newInstance(index);
newFragment.show(getFragmentManager(), "dialog");
}
在DialogFragment类中:
static MyDialogFragment newInstance(int index) {
MyDialogFragment f = new MyDialogFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int index = getArguments().getInt("index");
AlertDialog.Builder builder;
Dialog dialog;
builder = new AlertDialog.Builder(getActivity());
final Cursor c = someDatabaseHelper.getData(index);
builder.setCursor(c, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
c.moveToPosition(which);
int idWeWant = c.getInt(c.getColumnIndexOrThrow(STRING_ID_WE_WANT));
//you can make another dialog here using the same method
}
});
dialog = builder.create();
return builder.create();
}