在Android中将列表传递给另一个活动

我已经创build了一个列表,并希望将列表传递给另一个活动,但是当我创buildintent时,我在putExtra语句上出现错误。 只是想知道是否有任何简单的方法来传递一个string列表,而不是一个单一的string?

谢谢

private List<String> selItemList; private ListView mainListView = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.recipes); Button searchBtn = (Button) findViewById(R.id.searchButton); searchBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (selItemList == null) { Toast.makeText(getApplicationContext()," Please Make A Selection ", Toast.LENGTH_SHORT).show(); } else { Intent intent = new Intent(Recipes2.this, XMLParser.class); intent.putExtra("items_to_parse", selItemList); startActivityForResult(intent, 0); } } }); 

Related of "在Android中将列表传递给另一个活动"

你不能在Intent.putExtras(String name, List<?> list);传递List Intent.putExtras(String name, List<?> list); 。 我想你可以使用一个String Array ,并像putExtras这样传递它:

 private List<String> selItemList; private ListView mainListView = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.recipes); Button searchBtn = (Button) findViewById(R.id.searchButton); searchBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (selItemList == null) { Toast.makeText(getApplicationContext(), "Please Make A Selection", Toast.LENGTH_SHORT).show(); } else { String[] selItemArray = new String[selItemList.size()]; // Copy your List of Strings into the Array, and then pass it in your intent // .... Intent intent = new Intent(Recipes2.this, XMLParser.class); intent.putExtra("items_to_parse", selItemArray); startActivityForResult(intent, 0); } } }); 

你可以使用Intent

public Intent putStringArrayListExtra(String name,ArrayList value)

  private final List<String> selItemList; private ListView mainListView = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.recipes); Button searchBtn = (Button) findViewById(R.id.searchButton); searchBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (selItemList == null) { Toast.makeText(Recipes2.this," Please Make A Selection ", Toast.LENGTH_SHORT).show(); } else { Intent intent = new Intent(Recipes2.this, XMLParser.class); intent.putStringArrayListExtra("items_to_parse", (ArrayList<String>) selItemList); startActivityForResult(intent, 0); } } }); 

在你的XMLParser.class中:

  public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getIntent().getExtras() != null) { for(String a : getIntent().getExtras().getStringArrayList("items_to_parse")) { Log.d("=======","Data " + a); } } 

我知道这有点晚了,这个问题已经有了答案,但这是另一种方式。

只需创build另一个对象,将其定义为Serializable并为其指定一个列表variables,然后使用putExtra以下intent发送:

 public class Category implements Serializable { private List<YourObject> objects; public Category() { } public List<YourObject> getObjects() { return objects; } public void setObjects(List<YourObject> objects) { this.objects = objects; } 

然后发送它这样做:

 Category cat = new Category(); cat.setObjects(objects); intent.putExtra("listOfObjects",cat); startActivity(intent); 

并获得你创build的对象做到这一点:

 Category cat = (Category) extras.get("listOfObjects"); cat.getObjects;