我有一个todolist应用程序并且不能将一个活动中的自定义列表传递给另一个。我使用数据包装器但是当单击添加按钮程序时会给出nullpointerexception。
这是AddItem.java
okButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
ArrayList<Entry> itemList = new ArrayList<Entry>();
task = editText1.getText().toString();
priorityLevel = spinner.getSelectedItem().toString();
status = toggleButton.getText().toString();
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1;
int year = datePicker.getYear();
date = day + "/" + month + "/" + year;
itemList.add(new Entry(task, priorityLevel, date, status));
Intent okIntent = new Intent();
okIntent.putExtra("item",new DataWrapper(itemList));
setResult(Activity.RESULT_OK, okIntent);
finish();
}
});
和MainActivity.java
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case ADD_NOTE:
Log.w("1", "one");
DataWrapper dw = (DataWrapper) getIntent().getSerializableExtra("item");
Log.w("2", "two");
entryList = dw.getEntryList();
Log.w("3", "three");
//String extraName = AddItem.code;
//ArrayList<Entry> list = data.getParcelableArrayListExtra(extraName);
todoItems.addAll(entryList);
Log.w("4", "four");
todoArrayAdapter.notifyDataSetChanged();
Log.w("5", "five");
break;
答案 0 :(得分:1)
ArrayList实现Serializable
您可以使用意图
ArrayList<String> mylist = new ArrayList<String>();
Intent intent = new Intent(ActivityName.this, Second.class);
intent.putStringArrayListExtra("key", mylist);
startActivity(intent);
要检索
Intent i = getIntent();
ArrayList<String> list = i.getStringArrayListExtra("key");
答案 1 :(得分:1)
实施Parcelable接口。 http://bimbim.in/post/2010/09/27/Android-Passing-object-from-one-activity-to-another.aspx
答案 2 :(得分:1)
替换
DataWrapper dw = (DataWrapper) getIntent().getSerializableExtra("item");
与
DataWrapper dw = (DataWrapper) data.getSerializableExtra("item");
onActivityResult()...