我在片段中有一个适配器,在这个适配器中,我正在为一个活动运行activityForResult
。
从Fragment
调用适配器mAdapClinicSchedule = new ClinicScheduleAdapter(getContext(),mScheduleList);
mLvClinicsSchedules.setAdapter(mAdapClinicSchedule);
适配器的构造函数:
public ClinicScheduleAdapter(Context context, ArrayList<String> mScheduleList) {
this.context = context;
this.mScheduleList = mScheduleList;
}
从适配器
调用startActivityForResult
case R.id.tvDaysClinicSchedule:
Intent intent = new Intent(context, ScheduleDaysActivity.class);
((Activity)context).startActivityForResult(intent, 2);
break;
活动中的setResult
case R.id.tvSelectDays:
checkDaysSelected();
Intent returnIntent = new Intent();
returnIntent.putExtra("monday", mondaySelected);
returnIntent.putExtra("tuesday", tuesdaySelected);
returnIntent.putExtra("wednesday", wednesdaySelected);
returnIntent.putExtra("thursday", thursdaySelected);
returnIntent.putExtra("friday", fridaySelected);
returnIntent.putExtra("saturday", saturdaySelected);
returnIntent.putExtra("sunday", sundaySelected);
setResult(Activity.RESULT_OK, returnIntent);
finish();
break;
创建onActivityResult
界面
public interface OnActivityResult {
void onActivityResult(int requestCode, int resultCode, Intent data);
}
实现适配器:
public class ClinicScheduleAdapter
extends BaseAdapter implements View.OnClickListener, OnActivityResult { ... }
在片段中,它会覆盖onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
mAdapClinicSchedule.onActivityResult(requestCode, resultCode, data);
//super.onActivityResult(requestCode, resultCode, data);
}
但在适配器中,它没有显示吐司,我不知道我做错了什么
public void onActivityResult (int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(context, "it worked!", Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:0)
当然,响应的活动必须设计为返回结果。当它发生时,它将结果作为另一个Intent对象发送。您的活动在onActivityResult()回调中收到它。
虽然,如果我没记错的话,onActivityResult()
会在开始结果活动时被调用。因此,如果您使用活动来启动新意图,则活动将接收结果调用。这意味着如果您希望片段处理它,您需要在片段而不是活动上调用startActivityForResult(...)