gpt4 book ai didi

android - 无法在 RecyclerAdapter 中设置方法 setResult

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:25:55 27 4
gpt4 key购买 nike

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

}
}

我需要怎么写?请帮助我并解释我做错了什么我需要从所有案例中设置结果

编辑:

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;
}
}
}

最佳答案

Can't set method setResult in RecyclerAdapter

setResultMyViewHolder 类中不可访问,因为 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 访问 Button onClick 中的 setResult

关于android - 无法在 RecyclerAdapter 中设置方法 setResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28560722/

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