RecyclerView - 更改单击项目上的文本

时间:2016-01-04 13:27:33

标签: android android-recyclerview

我想仅在单击按钮的特定项目中更改文本。 现在 - 只有最后一个项目文本正在改变。

我的代码:

class MyMenuAdapter extends RecyclerView.Adapter<MyMenuAdapter.ViewHolder> {
    private List<FoodItem> mDataset; /// the list of feedlistings for the adapter
    private Context context;
    private TextView foodTitle, foodPrice, foodType, foodNumber;
    private ImageView foodImage;
    private LinearLayout minusLayout, plusLayout;


    // Provide a reference to the views for each data item

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


        public ViewHolder(View v) {
            super(v);

            foodPrice = (TextView) v.findViewById(R.id.card_food_price);
            foodTitle = (TextView) v.findViewById(R.id.card_food_title);
            foodType = (TextView) v.findViewById(R.id.card_food_type);
            foodImage = (ImageView) v.findViewById(R.id.card_meal_image);
            foodNumber = (TextView) v.findViewById(R.id.card_food_number);
            minusLayout = (LinearLayout) v.findViewById(R.id.minus_food_button);
            plusLayout = (LinearLayout) v.findViewById(R.id.plus_food_button);
            minusLayout.setOnClickListener(this);
            plusLayout.setOnClickListener(this);


        }

        @Override
        public void onClick(View v) {

            int position = getAdapterPosition();


            switch (v.getId()) {


                case R.id.minus_food_button:


                    if (!foodNumber.getText().equals("0")) {

                        try {
                            int myNum;
                            myNum = Integer.parseInt(foodNumber.getText().toString()) - 1;
                            foodNumber.setText(myNum + "");
                        } catch (NumberFormatException nfe) {
                            System.out.println("Could not parse " + nfe);
                        }
                    }

                    break;


                case R.id.plus_food_button:


                    try {
                        int myNum;
                        myNum = Integer.parseInt(foodNumber.getText().toString()) + 1;
                        foodNumber.setText(myNum + "");
                    } catch (NumberFormatException nfe) {
                        System.out.println("Could not parse " + nfe);
                    }


                    break;

            }



        }
    }


    // Provide a suitable constructor (depends on the kind of dataset)
    public MyMenuAdapter(Context context, List<FoodItem> myDataset) {
        this.context = context;
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyMenuAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_list_item, parent, false);
        // set the view's size, margins, paddings and layout parameters
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }


    // set all the layout things here

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


        foodTitle.setText(mDataset.get(position).getFOOD_TITLE());
        foodType.setText(mDataset.get(position).getFOOD_TYPE());
        foodPrice.setText(mDataset.get(position).getFOOD_PRICE() + "");
        Picasso.with(context).load(mDataset.get(position).getFOOD_IMAGE()).into(foodImage);


    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }

}

已经尝试在onclick方法中创建textview的引用,但随后会发生nullPointerException。

2 个答案:

答案 0 :(得分:2)

您正在更改onClick中的ng-pattern,这使您受到回收者的支配。这就是为什么你只看到最后一项的变化。

执行此操作的方法是更新适配器模型中的值并调用TextView。通过查看notifyDataSetChanged(),您似乎没有在适配器的任何位置建模onBindViewHolder()的值。

首先,您需要在适配器中添加数据结构以保存foodNumber的当前值。然后在foodNumber中,将onBindViewHolder()的文本设置为该项目的当前值。在foodNumber中,您可以递增或递减适配器中该项目的当前值,然后调用onClick()(或notifyDataSetChanged()来代替刷新该视图)。这将使notifyItemChanged()刷新并致电RecyclerView,获取onBindViewHolder()的新值。

答案 1 :(得分:0)

请在此处阅读完整的解决方案,

如果你想改变textview特定位置的文本,那么它只能在你设置适配器的活动中完成。在ArrayList中只有适配器设置位置无关。

活动

       namespace SharedProject1{public class Class1() { }}