无法在RecyclerAdapter中设置方法setResult

时间:2015-02-17 11:16:17

标签: android recycler-adapter

在我的应用程序中,我需要启动 RecyclerAdapter.class 并等待结果,但我不能写行setResult并完成:

public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView title;
    ImageView icon;

    public MyViewHolder(View itemView) {
        super(itemView);

        title = (TextView) itemView.findViewById(R.id.list_title);
        icon = (ImageView) itemView.findViewById(R.id.list_item);
        icon.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        String selectedCountry;




        int country = getPosition();

        String ololo = Integer.toString(country);

        Log.i("WORKS" ,"Выюрана страна под номером" +ololo);
        switch (getPosition()) {
            case 0:
                selectedCountry = "ukr";
                Log.i ("1" ,"" +selectedCountry);
                Intent intent= new Intent();
                intent.putExtra("country", selectedCountry);


            break;
            case 1:
                selectedCountry = "by";
                Log.i ("1" ,"" +selectedCountry);
                break;
            case 2:
                selectedCountry = "rus";
                Log.i ("1" ,"" +selectedCountry);
                break;
            case 3:
                selectedCountry = "nrus";
                Log.i ("1" ,"" +selectedCountry);
                break;
        }



        //intent. putExtra("country")

    }
}

我需要怎么写?请帮助我并解释我做错了什么 我需要从所有情况中设置setResult

修改

public class RecyclerAdapterChooseCountry extends RecyclerView.Adapter<RecyclerAdapterChooseCountry.MyViewHolder> {

private LayoutInflater inflater;

List<CountryInformation> data = Collections.emptyList();

public RecyclerAdapterChooseCountry(Context context, List<CountryInformation> data) {
    inflater = LayoutInflater.from(context);
    this.data = data;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.country_list, parent, false);
    MyViewHolder holder = new MyViewHolder(view, mActivity /*<- cant add this*/ );
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

    CountryInformation information = data.get(position);

    holder.title.setText(information.title);
    holder.icon.setImageResource(information.iconId);

}

@Override
public int getItemCount() {
    return data.size();
}


public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView title;
    ImageView icon;

    private Activity mActivity;

    public MyViewHolder(View itemView, Activity mActivity  ) {
        super(itemView);

        title = (TextView) itemView.findViewById(R.id.list_title);
        icon = (ImageView) itemView.findViewById(R.id.list_item);
        icon.setOnClickListener(this);
        this.mActivity = mActivity;
    }

    @Override
    public void onClick(View v) {

        String selectedCountry;

        int country = getPosition();

        String ololo = Integer.toString(country);

        Log.i("WORKS", "Выюрана страна под номером" + ololo);
        switch (getPosition()) {
            case 0:
                selectedCountry = "ukr";
                Log.i("1", "" + selectedCountry);
                Intent intent = new Intent();
                intent.putExtra("country", selectedCountry);
                mActivity.setResult(mActivity.RESULT_OK, intent);
                mActivity.finish();

                break;
            case 1:
                selectedCountry = "by";
                Log.i("1", "" + selectedCountry);
                break;
            case 2:
                selectedCountry = "rus";
                Log.i("1", "" + selectedCountry);
                break;
            case 3:
                selectedCountry = "nrus";
                Log.i("1", "" + selectedCountry);
                break;
        }
    }
}

}

5 个答案:

答案 0 :(得分:7)

  

无法在RecyclerAdapter中设置方法setResult

setResult类中无法访问

MyViewHolder因为setResult在Activity类中定义了

在创建适配器对象时,用于从Adapter构造函数中的非Activity类传递Activity上下文访问setResult

private Activity mActivity;
public MyViewHolder(View itemView,Activity mActivity) {
        super(itemView);
        /...your code here..
        this.mActivity=mActivity;
 }

使用mActivity访问按钮内的setResult

答案 1 :(得分:2)

您可以通过将活动作为参数传递给setResult()来使用finish()RecyclerAdapter,然后使用activity.setResult()activity.finish()

进行访问

答案 2 :(得分:0)

您可以从上下文中获取活动

zip

答案 3 :(得分:0)

Activity对象和Context对象之间是有区别的(如果有混淆,请阅读文档以更好地理解) 因此,答案很简单,

您想执行基于活动的操作,请按照以下步骤操作

Activity activity = this;
Context context = ActivityName.this;

在适配器中传递两个对象,然后使用

 activity.setResult(Activity.RESULT_OK,intent);
 activity.finish();

并用于基于上下文的操作

Intent i = new Intent(ListViewAdapter.this.mContext,EventPrivacyAndTicketPrice.class);

查看下面的完整活动代码:

public class EventType extends AppCompatActivity {
Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_ticket_type_and_price);
        intent = getIntent();
        ListView listView = findViewById(R.id.event_type_list_view);
        ArrayList<ETypeObject> allTypes= new ArrayList<>();
        allTypes = GetData();

        ListViewAdapter listViewAdapter= new       ListViewAdapter(allTypes,EventType.this,intent,this);
        listView.setAdapter(listViewAdapter);

    }

    public ArrayList<ETypeObject> GetData(){
        ArrayList<ETypeObject> allTypes= new ArrayList<>();
        allTypes.add(new ETypeObject("Conference",0));
        allTypes.add(new ETypeObject("Seminar, Talk",0));
        allTypes.add(new ETypeObject("Tradeshow, Expo, Product Launch",0));

   return allTypes;
    }


}

class ETypeObject {
   public String name;
    public int imageId;

    public ETypeObject(String name, int imageId) {
        this.name = name;
        this.imageId = imageId;
    }

    public ETypeObject() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }
}




 class ListViewAdapter extends BaseAdapter{
    Activity activity;
    Context mContext;
    ArrayList<ETypeObject> allTypes= new ArrayList<>();
    Intent intent;
    public ListViewAdapter(){}

    public ListViewAdapter(ArrayList<ETypeObject> AllTypes, Context context,Intent intent,Activity activity){
        mContext = context;
        allTypes = AllTypes;
        activity =activity;
        this.intent = intent;

    }



    @Override
    public int getCount() {
        return allTypes.size();
    }

    @Override
    public Object getItem(int i) {
        return allTypes.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i1, View view, ViewGroup viewGroup) {

        LayoutInflater layoutInflater= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (layoutInflater!=null){
            view = layoutInflater.inflate(R.layout.lay1,null);

        }

        TextView type;


        type = view.findViewById(R.id.parentInteresttext);

        type.setText(allTypes.get(i1).getName());

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               // Toast.makeText(mContext,allTypes.get(i1).getName(),Toast.LENGTH_LONG).show();

                if (intent.getBooleanExtra("fromThirdActivty",false)){

                    Intent i = new Intent();
                    i.putExtra("eventType",allTypes.get(i1).getName());
                    activity.setResult(Activity.RESULT_OK,intent);
                    activity.finish();

                }


                Intent i = new Intent( ListViewAdapter.this.mContext,OtherActivity.class);

                i.putExtra("eventType",allTypes.get(i1).getName());
                ListViewAdapter.this.mContext.startActivity(i);
            }
        });
        return view;
    }
}

答案 4 :(得分:0)

对于Java:

Intent intent = new Intent();                
intent.putExtra("dataKey","DataValue");
((YOURACTIVITY)context).setResult(2,intent);

对于 Kotlin:

(context as YourActivity).setResult()