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

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

问题描述

我已经创建了一个清单,并希望列表传递到另一个活动,但我发现在putExtra声明一个错误,当我创建的意图。只是想知道有没有什么简单的方法来传递字符串列表,而不是一个单独的字符串?

感谢

 私人列表<字符串> selItemList;
私人的ListView mainListView = NULL;

    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.recipes);
        按钮searchBtn =(按钮)findViewById(R.id.searchButton);
        sea​​rchBtn.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            如果(selItemList == NULL){
                Toast.makeText(getApplicationContext(),请进行选择,Toast.LENGTH_SHORT).show();
            } 其他 {
                意向意图=新的意图(Recipes2.this,XMLParser.class);
                intent.putExtra(items_to_parse,selItemList);
                startActivityForResult(意向,0);
            }
        }
        });
 

解决方案

你不能传递Intent.putExtras列表(字符串名称,列出清单); 我想你可以使用一个String数组,并通过它putExtras是这样的:

 私人列表<字符串> selItemList;
私人的ListView mainListView = NULL;

公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.recipes);

    按钮searchBtn =(按钮)findViewById(R.id.searchButton);
    sea​​rchBtn.setOnClickListener(新OnClickListener(){
    公共无效的onClick(视图v){
        如果(selItemList == NULL){
            Toast.makeText(getApplicationContext(),请进行选择,Toast.LENGTH_SHORT).show();
        } 其他 {
            的String [] selItemArray =新的String [selItemList.size()];
            //复制你的字符串列表到数组,然后通过它在你的意图
            // ....
            意向意图=新的意图(Recipes2.this,XMLParser.class);
            intent.putExtra(items_to_parse,selItemArray);
            startActivityForResult(意向,0);
        }
    }
    });
 

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?

Thanks

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

解决方案

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天全站免登陆