将ArrayList <string>从BaseAdapter发送到Activity

时间:2015-07-02 09:53:56

标签: android android-intent arraylist

我正在尝试通过intent.putExtra将ArrayList发送到我的Activity。在我的Activity i中,我只看到位置(int)成功到达,但ArrayList是空的。

  ArrayList<String> resultTitle, resultText; 

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
                View rowView;
                rowView = inflater.inflate(R.layout.news_adapter_listview_layout, null);

                if (resultTitle.get(position) != null) {
                    resultTitleTV = (TextView) rowView.findViewById(R.id.firstLine);
                    resultTitleTV.setText(resultTitle.get(position));

                }
                if (resultText.get(position) != null) {

                    resultTextTV = (TextView) rowView.findViewById(R.id.secondLine);
                    resultTextTV.setText(br2nl(resultText.get(position)).substring(0, 50) + "....");

                }

                rowView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(context, TheNews.class);
                        intent.putExtra("position", position);
                        intent.putExtra("Title", resultTitle);
                        intent.putExtra("Text", resultText);
                        context.startActivity(intent);

                        Toast.makeText(context, "You Clicked " + resultTitle.get(position), Toast.LENGTH_LONG).show();
                    }
                });

                return rowView;
            }

这就是我的Activity获取BundleData的方式:

 Bundle extras = getIntent().getExtras();
        int position;
           if (extras != null) {
            position = extras.getInt("position");
            newsTitle = extras.getStringArrayList("Title");
            newsText = extras.getStringArrayList("Text");

            Toast.makeText(getApplication(), "You have clicked position Number: " + position, Toast.LENGTH_LONG).show();

            if (newsText.get(position) != null) {

                text = br2nl(newsText.get(position));    
                toolbarTheNews.setTitle(newsTitle.get(position));
                descriptionTV.setText(text);
            }
        }

现在可行

好的,谢谢大家,现在它有效。我刚刚将Activity中的ArrayList更改为static。

static ArrayList<String> newsText;
static ArrayList<String> newsTitle;

Bundle extras = getIntent().getExtras();
    int position;
    if (extras != null) {
        position = extras.getInt("position");
        newsTitle = extras.getStringArrayList("Title");
        newsText = extras.getStringArrayList("Text");

        Toast.makeText(getApplication(), "You have clicked position Number: " + position, Toast.LENGTH_LONG).show();

        if (newsText.get(position) != null) {

            text = br2nl(newsText.get(position));
            toolbarTheNews.setTitle(newsTitle.get(position));
            descriptionTV.setText(text);
        }
    }

在BaseAdapter中,我发送如下的值:

Intent intent = new Intent(context, TheNews.class);
intent.putExtra("position", position);
intent.putExtra("Title", resultTitle);
intent.putExtra("Text", resultText);
context.startActivity(intent);

我没有将putExtra更改为putStringArrayListExtra并仍然有效。但两者之间的区别是什么?

3 个答案:

答案 0 :(得分:1)

如果您想使用BundlegetStringArrayList()中提取数据,则需要使用Intent将额外内容放入putStringArrayListExtra()

答案 1 :(得分:1)

你想存储StringArrayList然后使用

<强> putStringArrayListExtra

 Intent intent = new Intent(this,activityName);
 intent.putStringArrayListExtra("key", value)

 context.startActivity(intent);

第二个活动你通过

获得价值

<强> getStringArrayListExtra

  Intent intent = getIntent();
  intent.getStringArrayListExtra("key");

答案 2 :(得分:1)

使用意图

BaseAdapter 传递ArrayList 活动

ArrayList<String> mList = new ArrayList<String>();

将ArrayList 放入 BaseAdapter

中的意图
Intent i1 = new Intent(MyCurrentActivity.this, MyNextActivity.class);
i1.putStringArrayListExtra("list_key", mList);
mContext.startActivity(i1);
来自活动意图

Retreive ArrayList

ArrayList<String> mList = new ArrayList<String>();


Intent i2 = getIntent();
mList = i2.getStringArrayListExtra("list_key");

for (String string : mList) {
    Log.i("Element", string);
}

完成