gpt4 book ai didi

android - 如何从 Bundle 获取值到另一个 Fragment

转载 作者:搜寻专家 更新时间:2023-11-01 07:58:35 25 4
gpt4 key购买 nike

我有问题将 bundle 的值(value)获取到另一个 fragment 我的代码正在工作但是从 SQLite Database 获取不同的数据例如我登录后登录我的帐户来自其他帐户的不同数据显示在我的 Profile.class 中,如果有人可以告诉我如何从 bundle 中获取值到另一个 Fragment

这是我的 Login.class 中的内容

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
DocProfile fragment = new Profile();
Bundle bundle = new Bundle();
bundle.putString("Name", c.getString(1));
bundle.putString("Age", c.getString(2));
bundle.putString("Gender", c.getString(3));
bundle.putString("Address", c.getString(4));
bundle.putString("Email", c.getString(5));
bundle.putString("Contact", c.getString(6));
fragment.setArguments(bundle);
ft.replace(R.id.content_frame, fragment);
ft.commit();

在我的 Profile.class 中从 bundle 中获取值但获取不同的用户数据

Bundle args = getArguments();
if (args != null && args.containsKey("Name"))
name = args.getString("Name");

if (args != null && args.containsKey("Age"))
age = args.getString("Age");

if (args != null && args.containsKey("Gender"))
gender = args.getString("Gender");

if (args != null && args.containsKey("Address"))
address = args.getString("Address");

if (args != null && args.containsKey("Email"))
email = args.getString("Email");

if (args != null && args.containsKey("Contact"))
contact = args.getString("Contact");

最佳答案

嘿,当你尝试从游标中获取值时,试试这样

我认为问题是你在 c.getString(int) 中传递的索引号

bundle.putString("Name", c.getString(c.getColumnIndex("your_column_name")));

@Sanket Kachhela 的其他人会指导您这样做

建议

当你在循环中获取游标值时使用这个

bundle.putString("Name", c.getString(c.getColumnIndex("your_column_name")));

喜欢

 int columnIndex = c.getColumnIndex("your_column_name");
bundle.putString("Name", c.getString(columnIndex));

因为

每次在循环中查找列索引是一项耗时的任务,这会影响应用程序性能,而列索引不会每次都更改,因此每次都在循环中找到它并使用它

关于android - 如何从 Bundle 获取值到另一个 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24583705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com