【发布时间】:2016-02-28 02:16:22
【问题描述】:
我有一个Recyclerview,它使用包含Arraylist 的资源文件填充数据。
我有第二个活动,其中包含一个 Edidtext 和一个 Button,它将值插入到第二个活动中设置的另一个 Arraylist。
我需要在第二个活动中使用Arraylist 来填充Recyclerview,而不是使用资源文件的数据。
我尝试使用Intent 将一个活动的Arraylist 传递给另一个活动,但没有成功。
ViewHolder 中的ArrayList
public ListAdapter(Context context) {
mContext = context;
final String[] countryNames = context.getResources().getStringArray(R.array.country_names);
mItems = new ArrayList<>();
for (int i = 0; i < countryNames.length; i++) {
mItems.add(new LineItem(countryNames[i]));
}
}
我想用作数据的第二个活动中的 Arraylist
public class DisplayDescActivity extends AppCompatActivity {
private EditText descItem;
public static ArrayList<String> descList = new ArrayList<String>();
...
}
有没有办法做到这一点?
Intent 代码尝试:
-
DisplayDescActivity 代码:
在我使用过的protected void onCreate(Bundle savedInstanceState)上:Intent intent1 = new Intent(this, ListAdapter.class); intent1.putStringArrayListExtra("list", descLista); startActivity(intent1); -
ListAdapter 代码:
我尝试使用以下代码final ArrayList<String> resultArray = getIntent().getStringArrayListExtra("list");
但是按摩Cannot resolve method'getIntent()'出现了。
【问题讨论】:
-
你应该通过Intent传递它,为什么你没有成功?您可以发布您尝试过的代码吗?
-
@Nanoc 我已经更新了 Intent 代码。提前致谢。
-
我不知道你试图在哪里调用getIntent,但你需要活动参考,可能你在一个片段内?然后使用 getActivity().getIntent()
-
@Nanoc 实际上,我不知道在哪里调用 getIntent。我的代码中没有片段。这是 ViewHolder 代码:link,我应该把它放在哪里?
-
正如我在您的代码中看到的,您应该能够做到 ((Activity)mContext).getIntent()
标签: java android android-intent arraylist android-recyclerview