将 ArrayAdapter 发送到另一个活动的代码?

2023-12-15

因为我们可以像这样将 String 类型发送到另一个活动

public static final String EXTRA_MESSAGE = 
               "com.example.android.twoactivities.extra.MESSAGE";

这个的代码应该是什么

private static final ArrayAdapter LIST_OF_CUSTOMERS = 

P.S.-我正在 MainActivity 中编写此代码,并希望以 ListView 的形式将数据库发送到另一个名为 saveScreen 的活动


我的第一个建议是为什么第二个活动不能简单地查询数据库本身?

除此之外,如果您必须的话,我建议您从ArrayAdapter进入一个ArrayList并使用Bundle.putParcelableArrayList当调用第二个活动时。

See here or this用于将 Bundles 传递给 Activity 并再次读取它们的值,但本质上您在调用第二个 Activity 时会执行类似的操作:

Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("LIST_OF_CUSTOMERS", arrayListOfCustomers);
intent.putExtras(bundle);
startActivity(intent);

在第二个 Activity 中:

ArrayList<..> customers = getActivity().getIntent().getParcelableArrayListExtra<..>("LIST_OF_CUSTOMERS");
if (customers != null) {
    // do something with the data
}

唯一要记住的是,无论您的列表是什么类型,即ArrayList<Customer>, Customer类需要实现Parcelable界面。看here or here了解更多。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 ArrayAdapter 发送到另一个活动的代码? 的相关文章

随机推荐