用于将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
并使用
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.
请参见此处或
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>
,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屋!