无法使用之前设置的按钮更改customAdapter中的textview值,为什么?

时间:2016-02-28 15:01:18

标签: android listview custom-adapter

我已经从像这个

这样的ArrayList设置了textView的值
        mealCounterTV = (TextView) view.findViewById(R.id.counterTV);
        mealCounterTV.setText(quantityList.get(position));

有一个按钮,通过点击我试图更改textview值,值正在改变但无法将更改的值设置为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();
            }
        });

谁能指出我做错了什么? 我的整个适配器是这样的,

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

}

2 个答案:

答案 0 :(得分:3)

尝试从点击侦听器中删除notifyDataSetChanged。因为这会导致适配器再次重新绑定列表中的值并将列表重置为初始状态。

答案 1 :(得分:0)

设置文本后是否有notifyDataSetChanged()的原因?因为每次执行此操作时,它都将使用您传递的数据集中的值覆盖新的textView值。