gpt4 book ai didi

java - Android - 尝试从 Activity 2 到 Activity 1 以及从方法到方法获取数据

转载 作者:行者123 更新时间:2023-12-02 11:41:48 24 4
gpt4 key购买 nike

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

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

我可以将 EditText 的数据保存到字符串 dataFromActivity2 中,但是我应该如何将其从 onAddEventClicked 方法传递回 onAddEventClicked 方法,并设置 event.setName(dataFromActivity2) ?

不幸的是,我已经尝试了很多次了,但没有成功。您能给我一些提示/技巧吗?我知道解决方案可能很简单,但我仍然是初学者...:(我应该使用 SharedPreferences 代替 startActivityForResult 吗?

Activity 1:

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();
}

...
}

Activity 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();
}});}}

最佳答案

您应该将用于创建事件的代码移至 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);
}

关于java - Android - 尝试从 Activity 2 到 Activity 1 以及从方法到方法获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48487791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com