1

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

}
nazmul
  • 367
  • 2
  • 14

2 Answers2

3

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.

0

Is there a reason that you are doing notifyDataSetChanged() after setting the text? Because every time you do that, it will override your new textView value with the one in the dataset you are passing.

Akshay
  • 806
  • 11
  • 26