can we call startActivityForResult from adapter?

后端 未结 4 693
无人共我
无人共我 2021-02-03 19:10

is it possible to have method onActivityResume within adapter & call startActivityForResult?

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-03 19:35

    Not necessarily pass to pass context in adapter's constructor. You can get context from parent ViewGroup. Sample for RecyclerView adapter:

     Context mContext;
     @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            mContext = parent.getContext();
            ...
        }
    

    Sample for ListView BaseAdapter

     Context mContext;
     @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            mContext = parent.getContext();
            ...
    }
    

    And use it wherever you want

    ((Activity) mContext).startActivityForResult(intent, REQUEST_FOR_ACTIVITY_CODE);
    

提交回复
热议问题