Android - 尝试将数据从活动2恢复到活动1并从方法返回到方法

时间:2018-01-28 15:09:32

标签: java android android-edittext sharedpreferences

首先 - 我已经检查了不同的教程,但无法完成我的任务:(

我从活动1中的 onAddEventClicked 方法调用Activity 2打开布局,让用户在EditText中键入名称。然后我想关闭活动2,将捕获的名称发送回活动1,并在ArrayList中创建新的事件时使用它来设置名称参数: event.setName(dataFromActivity2)

我能够将EditText的数据保存到字符串 dataFromActivity2 ,但我应该如何将它从 onAddEventClicked 方法传回 onAddEventClicked 方法并设置event.setName(dataFromActivity2)?

不幸的是,我已经尝试了很多次而没有运气。你能指导我提出任何提示/技巧吗?我知道解决方案可能很简单,但我还是初学者... :(我应该使用 startActivityForResult 的SharedPreferences吗?

ACTIVITY1:

public class BaseActivity extends AppCompatActivity implements WeekView.EventClickListener, MonthLoader.MonthChangeListener, WeekView.EventLongPressListener, WeekView.EmptyViewLongPressListener, WeekView.EmptyViewClickListener, WeekView.AddEventClickListener {
private static final int TYPE_DAY_VIEW = 1;
private static final int TYPE_THREE_DAY_VIEW = 2;
private static final int TYPE_WEEK_VIEW = 3;
private int mWeekViewType = TYPE_THREE_DAY_VIEW;
private static final int REQUEST_CODE = 0;
String TAG = "***";
private WeekView mWeekView;
private ArrayList<WeekViewEvent> mNewEvents;
...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
        String dataFromActivity2= data.getStringExtra("fav");
}


@Override
public void onAddEventClicked(Calendar startTime, Calendar endTime) {

Intent intent = new Intent(this, Test2.class);
startActivityForResult(intent, REQUEST_CODE);

WeekViewEvent event = new WeekViewEvent();
event.setId(7);
event.setName(fav);
event.setStartTime(startTime);
event.setEndTime(endTime);
event.setColor(getResources().getColor(R.color.event_color_03));

Log.d(TAG, "Intent test:" +fav);
mNewEvents.add(event);
mWeekView.notifyDatasetChanged();
}

...
}

活动2:

public class Test2 extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);

    Button btn = (Button) findViewById(R.id.btn_test);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            EditText et = (EditText)findViewById(R.id.etv_test);
            String favourities=et.getText().toString();

            Intent intent=new Intent();
            intent.putExtra("fav", favourities);

            setResult(RESULT_OK,intent);
            finish();//finishing activity
            Toast.makeText(Test2.this, "INTENT CONTAINS:" + intent.getExtras(), Toast.LENGTH_SHORT).show();
        }});}}

1 个答案:

答案 0 :(得分:1)

您应该将用于创建事件的代码移动到onActivityResult方法中:

   private Calendar startTime, endTime;
   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            String dataFromActivity2 = data.getStringExtra("fav");
            // create event here
            WeekViewEvent event = new WeekViewEvent();
            event.setId(7);
            event.setName(dataFromActivity2);
            event.setStartTime(startTime);
            event.setEndTime(endTime);
            event.setColor(getResources().getColor(R.color.event_color_03));

            Log.d(TAG, "Intent test:" + dataFromActivity2);
            mNewEvents.add(event);
            mWeekView.notifyDatasetChanged();
    }

在onAddEventClicked中,您必须将其参数(startTime,endTime)保存到实例变量 - 才能在onActivityResult中使用它们:

    @Override
    public void onAddEventClicked(Calendar startTime, Calendar endTime) {
        this.startTime = startTime;
        this.endTime = endTime;
        Intent intent = new Intent(this, Test2.class);
        startActivityForResult(intent, REQUEST_CODE);
    }