【问题标题】:Passing a List to another Activity in Android将列表传递给 Android 中的另一个 Activity
【发布时间】:2011-08-30 12:58:38
【问题描述】:

我创建了一个列表并希望将列表传递给另一个活动,但是当我创建意图时,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,Eclipse 不会让我编译上面的。错误是“Intent 类型中的方法 putExtra(String, boolean) 不适用于参数 (String, List)”?

标签: android android-activity parameter-passing android-intent


【解决方案1】:

您可以从Intent 使用putStringArrayListExtra

公共意图 putStringArrayListExtra (字符串名称,ArrayList 值)

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

【讨论】:

  • 嗨 Ccheneson,我尝试了您的解决方案,但它在 putStringArrayListExtra 行上失败并出现类转换异常:05-22 12:11:09.267: ERROR/AndroidRuntime(2279): java.lang.ClassCastException: java.util.Arrays$ArrayList
  • 感谢您的回答!这个解决方案非常适合我!
  • TypeCasting 到 (ArrayList&lt;String&gt;) 真的很优雅。这些天来,我从来不知道我们可以在通过意图时进行 TypeCast。
【解决方案2】:

您不能在Intent.putExtras(String name, List&lt;?&gt; 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);              
        }
    }
});

【讨论】:

  • 我还推荐一个字符串数组。
【解决方案3】:

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

只需创建另一个对象,将其定义为Serializable,并为其提供一个列表变量,然后在您的intent 上使用putExtra 发送它,如下所示:

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;

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
  • 2017-04-05
  • 2016-01-14
相关资源
最近更新 更多