RecyclerView中的微调器可更新项目值

时间:2018-08-22 14:49:04

标签: android-recyclerview android-spinner

如何从Spinner中的RecyclerView选择中更新数据集中的值?我应该将LiveData和Room与notifyItemChange()一起使用,数据绑定还是更简单的方法?我有一个Horse类和一个HorseData类。项目数是静态的,变量的选项是静态的。我不需要添加值或项目,我只需要能够为每个项目选择变量WinPercentHorse并将该值保存回数据集。

public class HorseAdapter extends RecyclerView.Adapter<HorseViewHolder>  {

private AdapterView.OnItemSelectedListener onItemSelectedListener;

    //we are storing all the horses in a list
    private List<Horse> horseList;

    //this context we will use to inflate the layout
    private Context mCtx;


    //getting the context and product list with constructor
    public HorseAdapter(Context mCtx, List<Horse> horseList) {
        this.mCtx = mCtx;
        this.horseList = horseList;
    }

    @Override
    public HorseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //inflating and returning our view holder
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.item_layout, null);
        return new HorseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final HorseViewHolder holder, final int position) {
        //getting the product of the specified position
        Horse horse = horseList.get(position);

        //binding the data with the viewholder views
        holder.ViewWinPercent.setText(String.valueOf(horse.getWinPercentHorse()));
        holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(horse.getImgHorse()));
        holder.ViewValueLine.setText(String.valueOf(horse.getValueOdds()));
        holder.itemView.setTag(horse.getId());
        holder.spinnerView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                // Update winPercent of item On selecting a spinner item
                Toast.makeText(mCtx, "Spinner Selection =" + l + "item selected =" + position, Toast.LENGTH_SHORT).show();

                //How to update the WinPercentHorse variable for this item in the horseList
                //Then refresh the item notifyItemChange(position, object payload)?
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

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

    public void setHorseList(List<Horse> horseList) {
        this.horseList = horseList;
        notifyDataSetChanged();
    }

}

class HorseViewHolder extends RecyclerView.ViewHolder  {

TextView ViewWinPercent, ViewValueLine;
    ImageView imageView;
    Spinner spinnerView;


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

        ViewWinPercent = itemView.findViewById(R.id.winPercentHorse);
        ViewValueLine = itemView.findViewById(R.id.valueLine);
        imageView = itemView.findViewById(R.id.imgHorse);
        spinnerView=itemView.findViewById(R.id.spinner);

    }

}

0 个答案:

没有答案