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

10

我创建了一个列表,想将该列表传递到另一个活动中,但是在创建意图时的putExtra语句中出现了错误。只想知道是否有更简单的方法可以传递一个字符串列表而不是单个字符串?

谢谢。

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);              
            }
        }
        });

你能把Logcat中的错误添加到你的问题中吗? - Houcine
嗨Houcine,Eclipse不让我编译上面的代码。错误是“The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, List<String>)”? - user676567
3个回答

20

你可以使用Intent的putStringArrayListExtra方法。

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);
            }
        }

1
嗨Ccheneson,我尝试了你的解决方案,但在putStringArrayListExtra行出现了类转换异常:05-22 12:11:09.267: ERROR/AndroidRuntime(2279): java.lang.ClassCastException: java.util.Arrays$ArrayList - user676567
类型转换为 (ArrayList<String>) 真的很优雅。我从来不知道在通过意图传递时我们可以进行类型转换,所有这些天都不知道。 - vss

9

Intent.putExtras(String name, List<?> list);中,你无法传递一个List。

我认为你可以使用一个StringArray,并像这样通过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);              
        }
    }
});

我也建议使用字符串数组。 - keyser

4

我知道现在有一个答案了,但是这里还有另一种方法。

只需创建另一个对象,将其定义为Serializable并给它一个列表变量,然后使用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);

获取所创建的对象,请执行以下操作:

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

java.lang.RuntimeException: 在写入可序列化对象时遇到了Parcelable IOException异常。 - Yaroslav Dukal
一些对象不可序列化。请查看文档以了解哪些对象是或不是可序列化的。 https://developer.android.com/reference/java/io/Serializable.html - Pouya Danesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接