如何将对象的ArrayList发送到其他活动



我想知道如何在第二个活动中创建列表视图

Person person = new Person(nome, idade, numero);
this.list.add(person);
int number= this.list.size();
this.numero.setText(String.valueOf(number));
this.item.setText("");
}
break;
case R.id.btn_activity2:
Intent intent = new Intent(MainActivity.this, ListActivity.class);
intent.put(...here is the problem)
startActivity(intent);
break;

ArrayList发送到下一个活动的一种方法是创建一个bundle,如下所示:

Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Object", listOfObject);
intent.putExtras(bundle);
startActivity(intent);

listOfObject应替换为您的ArrayList的名称。然后,要在ListActivity.class中接收意向,您可以执行以下操作:

List<Person> myList= this.getIntent().getExtras().getParcelableArrayList("Object");

myList现在是该对象的列表。如果您收到任何错误,请在下面发表评论。

您可以使用intent.putStringArrayListExtra(your-key, ArrayList),然后使用获取

String[] arrayList = extras.getStringArrayListExtra(your-key);

最新更新