我想通過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和仍然有效。但他們之間有什麼區別?