Search code examples
androidlistviewcustom-adapter

Unable to change textview value from customAdapter using button once set before,why?


I have set the value of a textView from an ArrayList like this

        mealCounterTV = (TextView) view.findViewById(R.id.counterTV);
        mealCounterTV.setText(quantityList.get(position));

There is a button ,by clicking which i am trying to change the textview value ,the value is changing but unable to set the changed value to textView,

addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                count = Integer.parseInt(mealCounterTV.getText().toString());
                count++;
                mealCounterTV.setText(String.valueOf(count));
                notifyDataSetChanged();
            }
        });

can anyone point out where i am doing wrong? my whole adapter is like this,

package com.nerdcastle.nazmul.mealdemo;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;


class AdapterForMealSubmission extends BaseAdapter {
    ViewHolder holder;
    Activity activity;
    ArrayList<String> employeeNameList = new ArrayList<>();
    ArrayList<String> quantityList = new ArrayList<>();

    public AdapterForMealSubmission(Activity activity, ArrayList<String> employeeNameList, ArrayList<String> quantityList) {
        super();
        this.activity = activity;
        this.employeeNameList = employeeNameList;
        this.quantityList = quantityList;
    }

    public int getCount() {
        return quantityList.size();
    }

    public Object getItem(int position) {
        return quantityList.get(position);
    }

    public long getItemId(int position) {
        return 0;
    }

    private class ViewHolder {
        public TextView nameTV;
        public Button deleteBtn;
        public Button addBtn;
        public TextView mealCounterTV;
    }


    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = activity.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.custom_row_for_meal_submission, null);
            holder = new ViewHolder();
            holder.mealCounterTV = (TextView) convertView.findViewById(R.id.counterTV);
            holder.nameTV = (TextView) convertView.findViewById(R.id.nameTV);
            holder.deleteBtn = (Button) convertView.findViewById(R.id.minus_btn);
            holder.addBtn = (Button) convertView.findViewById(R.id.add_btn);
            convertView.setTag(holder);

            holder.addBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int count = Integer.parseInt(holder.mealCounterTV.getText().toString());
                    count++;
                    holder.mealCounterTV.setText("" + count);
                    notifyDataSetChanged();
                }
            });
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mealCounterTV.setText(quantityList.get(position));
        holder.nameTV.setText(employeeNameList.get(position));
        holder.addBtn.setTag(holder);
        holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = Integer.parseInt(holder.mealCounterTV.getText().toString());
                if (count > 0) {
                    count--;
                }
                holder.mealCounterTV.setText("" + count);
                notifyDataSetChanged();
            }
        });

        return convertView;
    }

}

Solution

  • Try removing the notifyDataSetChanged from the click listeners. Because this will cause the adapter to rebind the values in the list again and resetting the list to initial state.