gpt4 book ai didi

java - onActivityResult 未从适配器调用

转载 作者:行者123 更新时间:2023-12-02 09:06:01 28 4
gpt4 key购买 nike

每当我完成“ViewEvent” Activity 时,在“HomeFragment”中调用 onActivityResult() 时遇到问题。ViewEvents Activity 从适配器启动。 Activity 启动得很好,但是 onActivityResult 不会在 home fragment 中被调用。我怎样才能让它发挥作用?

主页 fragment

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Do stuff
EventListAdapter eventListAdapter = new EventListAdapter(getContext(), 0, events);
eventListView.setAdapter(eventListAdapter);
}

Activity 列表

 private Context mContext;

public EventListAdapter(@NonNull Context context, int resource, @NonNull List<WorkdayEvent> objects) {
super(context, resource, objects);
mContext = context;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
setupImageLoader();
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.event_list_layout,parent,false);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putSerializable("EVENT",event);
Intent intent = new Intent(mContext, ViewEvent.class);
intent.putExtra("EVENTBUNDLE",bundle);
Activity activity = (Activity)mContext;
activity.startActivityForResult(intent,DELETE_EVENT);
}
});
return convertView;
}

查看事件

        final Button deleteEvent = findViewById(R.id.buttonViewEventDelete);
deleteEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadEvents();
int index = getEventIndex(event);
if(index > -1) {
workdayEvents.remove(index);
}
saveEvents();
setResult(RESULT_OK);
finish();
}
});

最佳答案

您正在使用Activity.startActivityForResult,在这种情况下结果不会传播到 fragment 。

将 fragment 传递给适配器并使用 Fragment.startActivityForResult 来调用 onActivityResult

关于java - onActivityResult 未从适配器调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59805884/

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