onActivityResult上的宗地数据传输

时间:2017-05-30 18:38:00

标签: java android

我的包裹数据传输有问题。我不知道该怎么做 将对象parcelable转移到onActivityResult。 DreamDatabase实现了Parcelable。

MyDreams.class

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.action_addream:
            Intent intent = new Intent(getActivity(), AddDreamActivity.class);
            startActivityForResult(intent, REQUEST_CODE_CREATE);
            return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CODE_CREATE) {
                    DreamDatabase dreamDatabase = data.getData()
                    myDreamAdapter.addDream(dreamDatabase);
                    Toast.makeText(getActivity(), "Dreamd was added", Toast.LENGTH_SHORT).show();
        }
    }
}

AddDreams.class

if (!hasError) {
                    String selectDreamString = selectDreamTextView.getText().toString();
                    DreamDatabase dreamDatabase = new DreamDatabase(dreamTitleString, dreamDescriptionString, getCurrentDate(), selectDreamString);
                    Intent intent = new Intent();
                    intent.putExtra("dream", dreamDatabase);
                    setResult(RESULT_OK, intent);
                    super.finish();
                }

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

这个区块:

 if (requestCode == REQUEST_CODE_CREATE) {
                    DreamDatabase dreamDatabase = data.getData()
                    myDreamAdapter.addDream(dreamDatabase);
                    Toast.makeText(getActivity(), "Dreamd was added", Toast.LENGTH_SHORT).show();
        }

应改为:

DreamDatabase dreamDatabase = data.getParcelableExtra("dream");
...