从已滚出视图的ListView项目中丢失值

时间:2016-02-29 04:51:02

标签: android listview custom-adapter

我正在使用按钮,某种计数器更改textview值。例如,当我点击添加按钮时,值会增加,当单击减号按钮时它会减少,但是当项目滚动到视图之外时我正在失去这个价值,

适配器类:

   package com.nerdcastle.nazmul.mealdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Nazmul on 2/22/2016.
 */
public class AdapterForMealSubmission extends BaseAdapter implements ListAdapter {
    private ArrayList<String> employeeNameList;
    private ArrayList<String> quantityList;
    private Context context;
    int count = 0;


    public AdapterForMealSubmission(ArrayList<String> employeeNameList, ArrayList<String> quantityList, Context context) {
        this.employeeNameList = employeeNameList;
        this.quantityList = quantityList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return employeeNameList.size();
    }

    @Override
    public Object getItem(int pos) {
        return employeeNameList.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return pos;
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder holder;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.custom_row_for_meal_submission, null);
            holder = new ViewHolder();
            holder.nameTV = (TextView) view.findViewById(R.id.nameTV);
            holder.deleteBtn = (Button) view.findViewById(R.id.minus_btn);
            holder.addBtn = (Button) view.findViewById(R.id.add_btn);
            holder.mealCounterTV = (TextView) view.findViewById(R.id.counterTV);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.nameTV.setText(employeeNameList.get(position));
        holder.mealCounterTV.setText(quantityList.get(position));
        holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count = Integer.parseInt(holder.mealCounterTV.getText().toString());
                if (count > 0) {
                    count--;
                }
                holder.mealCounterTV.setText(String.valueOf(count));
                //notifyDataSetChanged();
            }
        });
        holder.addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count = Integer.parseInt(holder.mealCounterTV.getText().toString());
                count++;
                holder.mealCounterTV.setText(String.valueOf(count));
                //notifyDataSetChanged();
            }
        });

        return view;
    }
}

我怎么能停止失去价值?我已经阅读了类似的问题答案,但我不理解解决方案。

0 个答案:

没有答案