将 ArrayAdapter 发送到另一个活动的代码? [英] Code for sending ArrayAdapter to another activity?
问题描述
因为我们可以像这样将 String 类型发送到另一个活动
As we can send String type to another activity like this
public static final String EXTRA_MESSAGE =
"com.example.android.twoactivities.extra.MESSAGE";
这个代码应该是什么
private static final ArrayAdapter LIST_OF_CUSTOMERS =
P.S.- 我正在 MainActivity 中编写此代码,并希望以 ListView 的形式将数据库发送到另一个名为 saveScreen 的活动
P.S.- I am writing this code in MainActivity and want to send Database in the form of ListView to another activity named saveScreen
推荐答案
我的第一个建议是为什么第二个活动不能简单地查询数据库本身?
My first suggestion would be why cant the second activity simply query the database itself?
否则,如果必须的话,我建议将 ArrayAdapter
的结果放入 ArrayList
并使用 Bundle.putParcelableArrayList 调用第二个活动时.
Other then that if you must I'd suggest getting the results from the ArrayAdapter
into an ArrayList
and using Bundle.putParcelableArrayList when calling the second activity.
请参阅此处或this 用于将捆绑包传递给活动并再次读取它们的值,但本质上您在调用第二个活动时会执行以下操作:
See here or this for passing Bundles to Activities and reading their values back again, but essentially you would do something like this when calling the second activity:
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("LIST_OF_CUSTOMERS", arrayListOfCustomers);
intent.putExtras(bundle);
startActivity(intent);
在第二个活动中:
ArrayList<..> customers = getActivity().getIntent().getParcelableArrayListExtra<..>("LIST_OF_CUSTOMERS");
if (customers != null) {
// do something with the data
}
唯一要记住的是,无论您的列表是什么类型,即 ArrayList
,Customer
类都需要实现 Parcelable
界面.请参阅此处或此处了解更多信息.
The only thing to remember is that whatever type your list is of i.e. ArrayList<Customer>
, Customer
class needs to implement the Parcelable
interface. See here or here for more.
这篇关于将 ArrayAdapter 发送到另一个活动的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!