2015-07-02 67 views
0

我想通過intent.putExtra發送一個ArrayList到我的Activity。在我的Activity中,我只能看到position(int)成功到達,但ArrayList(s)是null。從BaseAdapter發送ArrayList <String>到活動

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

這是我的活動如何得到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); 
      } 
     } 

現在,它的工作原理

好感謝所有,現在它的工作原理。我只是將活動中的ArrayList更改爲靜態。

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

我didn't改變putExtra成putStringArrayListExtra和仍然有效。但他們之間有什麼區別?

回答

1

如果你想使用getStringArrayList()Bundle中提取數據,那麼你需要使用putStringArrayListExtra()將額外的內容放入Intent

1

ü要存儲StringArrayList然後用

putStringArrayListExtra

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

context.startActivity(intent); 

次活動U帶獲得價值

getStringArrayListExtra

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

通行證的ArrayListBaseAdapter活動使用意圖

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

完成

相關問題