可以从数据库中填充列表对话框吗?(Can a List Dialog be populated from the database?)

编程入门 行业动态 更新时间:2024-10-27 03:37:18
可以从数据库中填充列表对话框吗?(Can a List Dialog be populated from the database?)

我有一个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(); }

更多推荐

本文发布于:2023-07-28 01:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1298220.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   对话框   列表   List   populated

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!