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

Android 丢失已从视图中滚出的ListView项的值,android,listview,custom-adapter,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.ViewG

我正在使用按钮(某种计数器)更改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;
    }
}
package com.nerdcastle.nazmul.mealdemo;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.Button;
导入android.widget.ListAdapter;
导入android.widget.TextView;
导入java.util.ArrayList;
/**
*由Nazmul于2016年2月22日创建。
*/
公共类AdapterPerformeAlSubmission扩展BaseAdapter实现ListAdapter{
私人ArrayList员工姓名列表;
私有ArrayList quantityList;
私人语境;
整数计数=0;
公共AdapterPerformeAlSubmission(ArrayList employeeNameList、ArrayList quantityList、上下文){
this.employeeNameList=employeeNameList;
this.quantityList=quantityList;
this.context=上下文;
}
@凌驾
public int getCount(){
返回employeeNameList.size();
}
@凌驾
公共对象getItem(int-pos){
返回employeeNameList.get(pos);
}
@凌驾
公共长getItemId(int-pos){
返回pos;
}
私有静态类视图持有者{
公共文本视图名称电视;
公共按钮删除BTN;
公共按钮addBtn;
公共文本视图mealCounterTV;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视图=转换视图;
最终持票人;
如果(视图==null){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
视图=充气机。充气(R.layout.custom\u row\u用于提交膳食,空);
holder=新的ViewHolder();
holder.nameTV=(TextView)view.findViewById(R.id.nameTV);
holder.deleteBtn=(按钮)view.findViewById(R.id.减号\u btn);
holder.addBtn=(按钮)view.findViewById(R.id.add\u btn);
holder.mealCounterTV=(TextView)view.findViewById(R.id.counterTV);
视图.设置标签(支架);
}否则{
holder=(ViewHolder)view.getTag();
}
holder.nameTV.setText(employeeNameList.get(position));
holder.mealCounterTV.setText(quantityList.get(position));
holder.deleteBtn.setOnClickListener(新视图.OnClickListener()){
@凌驾
公共void onClick(视图v){
count=Integer.parseInt(holder.mealCounterTV.getText().toString());
如果(计数>0){
计数--;
}
holder.mealCounterTV.setText(String.valueOf(count));
//notifyDataSetChanged();
}
});
holder.addBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
count=Integer.parseInt(holder.mealCounterTV.getText().toString());
计数++;
holder.mealCounterTV.setText(String.valueOf(count));
//notifyDataSetChanged();
}
});
返回视图;
}
}

我如何才能停止丢失值?我已经阅读了类似的问题答案,但我不理解解决方案。

替换ArrayList employeeNameList;和ArrayList quantityList;而不是ArrayList employeeNameList=new ArrayList();ArrayList quantityList=新的ArrayList();那么什么时候初始化它们呢?我已经按照你的建议做了更改,仍然不起作用。你不需要初始化arraylist,因为在构造函数中,你在公共长getItemId(int position)函数中用传递paramsplace position来赋值,而不是0,如下所示。。。。公共长getItemId(int位置){return position;}