我正在开发用于餐厅管理的应用程序,但遇到了需要在RecyclerView内部单击项目时更新textview(在RecyclerView外部)值的问题。

这是我的适配器,考虑此适配器外部的textView并在加号和减号ImageViews的项单击上设置一个值,如下面的代码所示...

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyHolder> {

ArrayList<MenuItem> MenuDetailList;
Typeface font;

public MyRecyclerAdapter(ArrayList<MenuItem> MenuDetailArray){
    this.MenuDetailList = MenuDetailArray;
}

@Override
public MyRecyclerAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_menu_row_item, null);
    return new MyHolder(v);
}

@Override
public void onBindViewHolder(final MyRecyclerAdapter.MyHolder holder, final int position) {

    holder.txtSubMenuTitle.setText(MenuDetailList.get(position).getTitle());
    holder.txtSubMenuPrice.setText(MenuDetailList.get(position).getPrice());
    holder.txtSubMenuCount.setText(""+MenuDetailList.get(position).getItemCount());
    setCustomTypeface(holder.txtSubMenuTitle, font);
    setCustomTypeface(holder.txtSubMenuPrice,font);

    holder.ImgViewSubMenuMinus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //holder.OrderCount = Integer.parseInt(holder.txtSubMenuCount.getText().toString());
            int ItemCount = MenuDetailList.get(position).getItemCount();
            if (ItemCount > 0) {
                ItemCount = ItemCount - 1;
                holder.txtSubMenuCount.setText("" + ItemCount);
                MenuDetailList.get(position).setItemCount(ItemCount);
                notifyDataSetChanged();
            }
        }
    });

    holder.ImgViewSubMenuPlus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //holder.OrderCount = Integer.parseInt(holder.txtSubMenuCount.getText().toString());

            int ItemCount = MenuDetailList.get(position).getItemCount();
            if (ItemCount < 20) {
                ItemCount = ItemCount + 1;
                holder.txtSubMenuCount.setText("" + ItemCount);
                MenuDetailList.get(position).setItemCount(ItemCount);
                notifyDataSetChanged();
            }
    });
}

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

public static class MyHolder extends RecyclerView.ViewHolder {
    TextView txtSubMenuTitle;
    TextView txtSubMenuPrice;
    ImageView ImgViewSubMenuMinus;
    ImageView ImgViewSubMenuPlus;
    TextView txtSubMenuCount;

    private MyHolder(View row) {
        super(row);
        this.txtSubMenuTitle = (TextView) row.findViewById(R.id.list_txt_sub_menu_title);
        this.txtSubMenuPrice = (TextView) row.findViewById(R.id.list_txt_sub_menu_price);
        this.txtSubMenuCount = (TextView) row.findViewById(R.id.list_txt_sub_menu_count);
        this.ImgViewSubMenuMinus = (ImageView) row.findViewById(R.id.list_img_sub_menu_minus);
        this.ImgViewSubMenuPlus = (ImageView) row.findViewById(R.id.list_img_sub_menu_plus);
    }
}
private void setCustomTypeface(TextView textView, Typeface font) {
    textView.setTypeface(font);
}
}


提前致谢...

最佳答案

1,在你的活动中初始化TextView

TextView myTextview = (TextView) findViewByItd(R.id.textView);


2.在适配器的构造函数中添加参数TextView

3.将TextView传递给适配器

MyRecyclerAdapter adapter = new MyRecyclerAdapter(menu, myTextView);

4.根据需要进行操作

ArrayList<MenuItem> MenuDetailList;
Typeface font;
TextView mTextView;

public MyRecyclerAdapter(ArrayList<MenuItem> MenuDetailArray, TextView tv){
     this.MenuDetailList = MenuDetailArray;
     mTextView = tv;
}


mTextView.setText("Eat more pizza");

关于android - 更改Item上TextView的值在RecyclerView中单击吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35244257/

10-10 02:07