我有一个ListView,它包含一定数量的Names,当我从ListView中单击一个项目时,我希望弹出一个ListDialog,显示数据库中的某些数据。 那可能吗?
如果单击“列表”对话框中的项目后,如果“是”(如果可能),那么另一个列表对话框也可能会从中出现? 像列表对话框嵌套?
非常感谢!
I have a ListView that holds a certain number of Names, when I clicked an item from the ListView I want a ListDialog pop up displaying a certain data from the database. Is that possible?
If YES(if it is possible) after I click an item from the List Dialog it is also possible that another List Dialog will come out from it? Like List Dialog nested?
Thanks a lot!
最满意答案
是。 只需要一个新的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(); }Yes. Just get a new DialogFragment calling some newInstance() with some args to specify what you want.
In your list activity:
@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"); }In your DialogFragment class:
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(); }更多推荐
发布评论