将列表传递给 Android 中的另一个活动 [英] Passing a List to another Activity in Android

查看:27
本文介绍了将列表传递给 Android 中的另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个列表,并希望将该列表传递给另一个活动,但是当我创建意图时,我在 putExtra 语句中遇到错误.只是想知道有没有什么简单的方法可以传递字符串列表而不是单个字符串?

I've created a list and would like to pass the list to another activity but i'm getting an error on the putExtra statement when i create the intent. Just wondering is there any easy way to pass a List of Strings rather than a single 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);              
            }
        }
        });

推荐答案

Intent.putExtras(String name, List list); 中不能传递 List.我认为您可以使用 StringArray 并将其传递到 putExtras 中,如下所示:

You can't pass a List in Intent.putExtras(String name, List<?> list);. I think you can use an Array of String and pass it in putExtras like this:

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

这篇关于将列表传递给 Android 中的另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆