在我的应用程序中,我需要启动 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;
        }
    }
}

}

最佳答案



setResult类内部无法访问MyViewHolder,因为setResult是在Activity类内部定义的

要从非Activity类访问setResult,请在创建Adapter对象时在Adapter构造函数中传递Activity上下文:

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

使用mActivity在onClick的Button中访问setResult

09-26 08:25