Max
Max

Reputation: 309

RecyclerView - change text on clicked item

I want to change the text only in the specific item that I clicked a button on. right now - only the last`s item text is changing.

my code:

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

}

Already tried to create the reference of the textview inside the onclick method but then nullPointerException happends.

Upvotes: 2

Views: 6564

Answers (2)

bhumika rijiya
bhumika rijiya

Reputation: 440

please read full solution here,

if u want to change text on particular position of textview then it can be done only in activity in which you set adapter.there is nothing to do in adapter only set position from ArrayList.

In Activity

private Recycle_Adapter  adapter;

adapter.SetOnItemClickListener(new Recycle_FrameAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                     if(position == 1){
                        tv_module.setText("");
                    }
                    else if{

                         }
                       continue up to last


                }
            });

Upvotes: 0

kris larson
kris larson

Reputation: 30985

You are changing the TextView directly in your onClick, which puts you at the mercy of the recycler. That's why you only see the change in the last item.

The way to do this is to update a value in the adapter model and call notifyDataSetChanged(). From looking at your onBindViewHolder(), it seems that you don't have the value of foodNumber modeled anywhere in your adapter.

First you need to add a data structure in your adapter to hold the current values for foodNumber. Then in your onBindViewHolder(), set the text of foodNumber to the current value for that item. In your onClick(), you increment or decrement the current value for that item in the adapter, then call notifyDataSetChanged() (or notifyItemChanged() instead to refresh just that view). This will make the RecyclerView refresh and call onBindViewHolder(), picking up the new value for foodNumber.

Upvotes: 2

Related Questions