【发布时间】:2017-07-16 15:13:54
【问题描述】:
我想将 ArrayList 传递给新活动,在新活动中我将使用 ArrayAdapter 加载列表。
但我无法将对象转移到新活动中,我在新活动上得到了空值。
我读到我需要对 Person 类实现序列化... 这是唯一的方法吗?
这是我的代码:
AsyncTask onPostExecute 我正在获取数组。
@Override
protected void onPostExecute(ArrayList<Person> personArrayList){
Intent intent = new Intent(activity, ResultsActivity.class);
intent.putExtra("personArrayList",personArrayList);
activity.startActivity(intent);
}
使用 putExtra 发送。
这里是假设接收数组的 Activity。
public class ResultsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
Intent intent = getIntent();
ArrayList<Person> personArrayList = (ArrayList<Person>) intent.getSerializableExtra("personArrayList");
PeopleAdapter adapter = new PeopleAdapter(this, personArrayList);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// activity_results.xml layout file.
ListView listView = (ListView) findViewById(R.id.list);
// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Person} in the list.
listView.setAdapter(adapter);
}
}
所以在接收端 ArrayList 是空的。想法?
【问题讨论】:
-
是的,你需要在 Person 类上实现 Serializable
-
在ResultsActivity中获取intent时尝试这样。 personArrayList = getIntent().getStringArrayListExtra("personArrayList");
标签: java android android-intent arraylist