试图将数组列表解析为主要活动(android)

时间:2017-08-20 10:56:44

标签: android

这是我的mainActivity,我尝试使用startActivityForResult方法从editActivity获取结果,但是当我按下editActivity上的Add按钮时应用程序崩溃。我不确定我哪里出错了。

public class MainActivity extends AppCompatActivity {
private List list = new ArrayList<customButton>();
private static final int EDIT_ACTIVITY_RESULT_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // check that it is the SecondActivity with an OK result
    if (requestCode == EDIT_ACTIVITY_RESULT_CODE) {
        if (resultCode == RESULT_OK) {
            Bundle args = data.getBundleExtra("BUNDLE");
            list = (ArrayList<Object>) args.getSerializable("buttonList");
        }
    }
}
public void buttonEditOpen(View view) {
    Intent mintent = new Intent(MainActivity.this, EditActivity.class);
    Bundle args = new Bundle();
    args.putSerializable("buttonList",(Serializable)list);
    mintent.putExtra("BUNDLE",args);
    startActivityForResult(mintent, EDIT_ACTIVITY_RESULT_CODE);
}

这是我的第二个活动,它获取文本和创建一个名为customButton的对象的日期,该对象具有单个日期和名称以及一些信息,这是我正在尝试解析回mainActivity

public class EditActivity extends AppCompatActivity {
private List list = new ArrayList<customButton>();
private int selectedyear;
private int selectedmonth;
private int selectedday;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    Bundle args = intent.getBundleExtra("BUNDLE");
    list = (ArrayList<Object>) args.getSerializable("buttonList");
    setContentView(R.layout.activity_edit);
    final EditText etDate = (EditText) findViewById(R.id.editTextDueDate);
    etDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar mcurrentDate = Calendar.getInstance();
            int mYear = mcurrentDate.get(Calendar.YEAR);
            int mMonth = mcurrentDate.get(Calendar.MONTH);
            int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog mDatePicker = new DatePickerDialog(EditActivity.this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker datepicker, int tempSelectedYear, int tempSelectedMonth, int tempSelectedDay) {
                    // etDate.setText("" + tempSelectedYear + "/" + tempSelectedMonth + "/" + tempSelectedDay);
                    setClassDate(tempSelectedYear, tempSelectedMonth, tempSelectedDay);
                }
            }, mYear, mMonth, mDay);
            mDatePicker.setTitle("Select date");
            mDatePicker.show();
        }
    });
}

public void buttonAddClick(View view) {
    // getting information (name, date, info) from the activity_edit
    EditText etName = (EditText) findViewById(R.id.editTextName);
    EditText etDate = (EditText) findViewById(R.id.editTextDueDate);
    EditText etInfo = (EditText) findViewById(R.id.editTextInfo);
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    try {
        Date date = df.parse(etDate.getText() + "");
        customButton tempButton = new customButton(etName.getText() + "", date, etInfo.getText() + "");
        list.add(list.size(), tempButton);
        Intent mintent = new Intent();
        Bundle args = new Bundle();
        args.putSerializable("buttonList", (Serializable) list);
        mintent.putExtra("BUNDLE", args);
        setResult(RESULT_OK, mintent);
        finish();
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

public void setClassDate(int tempSelectedYear, int tempSelectedMonth, int tempSelectedDay) {
    this.selectedyear = tempSelectedYear;
    this.selectedmonth = tempSelectedMonth;
    this.selectedday = tempSelectedDay;
    EditText etDate = (EditText) findViewById(R.id.editTextDueDate);

    // Help from student teacher, using mode 100 returns two decimal numbers. Adding 0 to month and date less then 9

    if (selectedmonth % 100 > 9 && selectedday % 100 > 9) {
        etDate.setText(selectedday + "/" + selectedmonth + "/" + selectedyear);
    } else if (selectedmonth % 100 > 9) {
        etDate.setText("0" + selectedday + "/" + selectedmonth + "/" + selectedyear);
    } else if (selectedday % 100 > 9) {
        etDate.setText(selectedday + "/0" + selectedmonth + "/" + selectedyear);
    } else {
        etDate.setText("0" + selectedday + "/0" + selectedmonth + "/" + selectedyear);
    }
}
}

编辑请求customButton类的注释:

public class customButton {
private String _name;
private Date _dueDate;
private String _description;
public customButton(String _name, Date _dueDate, String _description) {
    this._name = _name;
    this._dueDate = _dueDate;
    this._description = _description;
}
public String get_name() {
    return _name;
}
public void set_name(String _name) {
    this._name = _name;
}
public Date get_dueDate() {
    return _dueDate;
}
public void set_dueDate(Date _dueDate) {
    this._dueDate = _dueDate;
}
public String get_description() {
    return _description;
}
public void set_description(String _description) {
    this._description = _description;
}
@Override
public String toString() {
    String output = _name + '\'' + _dueDate + '\''+_description;
    return output;
}
}

1 个答案:

答案 0 :(得分:1)

用此

替换现有的Model类
    public class customButton implements Parcelable{
    private String _name;
    private Date _dueDate;
    private String _description;
    public customButton(String _name, Date _dueDate, String _description) {
        this._name = _name;
        this._dueDate = _dueDate;
        this._description = _description;
    }

    protected customButton(Parcel in) {
        _name = in.readString();
        _description = in.readString();
    }

    public static final Creator<customButton> CREATOR = new Creator<customButton>() {
        @Override
        public customButton createFromParcel(Parcel in) {
            return new customButton(in);
        }

        @Override
        public customButton[] newArray(int size) {
            return new customButton[size];
        }
    };

    public String get_name() {
        return _name;
    }
    public void set_name(String _name) {
        this._name = _name;
    }
    public Date get_dueDate() {
        return _dueDate;
    }
    public void set_dueDate(Date _dueDate) {
        this._dueDate = _dueDate;
    }
    public String get_description() {
        return _description;
    }
    public void set_description(String _description) {
        this._description = _description;
    }
    @Override
    public String toString() {
        String output = _name + '\'' + _dueDate + '\''+_description;
        return output;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(_name);
        dest.writeString(_description);
    }
}

MainActivity类的变化

            @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // check that it is the SecondActivity with an OK result
        if (requestCode == EDIT_ACTIVITY_RESULT_CODE) {
            if (resultCode == RESULT_OK) {
                List<customButton> list= data.getParcelableArrayListExtra("BUNDLE");
            }
        }
    }

    public void buttonEditOpen(View view) {
    Intent mintent = new Intent(MainActivity.this, EditActivity.class);
    mintent.putParcelableArrayListExtra("BUNDLE", list);
    startActivityForResult(mintent, EDIT_ACTIVITY_RESULT_CODE);
}

现在终于编辑类中的更改

// OnCreate方法

super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    list = intent.getParcelableArrayListExtra("BUNDLE");

//在编辑类中设置结果像这样

Intent mintent = new Intent();
mintent.putParcelableArrayListExtra("BUNDLE", list);
setResult(RESULT_OK, mintent);
finish();