向下滚动listView时,复选框的计数器减少,但复选框的位置保持不变

时间:2016-11-20 17:15:45

标签: android listview checkbox nullpointerexception

我有一个 DeleteAdapter ,listView的每一行都包含textView和checkBox。 我还有一个 DeleteTask ,其中包含一个按钮,用于计算 DeleteAdapter 中的总复选框。

DeleteTask活动

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.delete_task);
        delete = (Button)findViewById(R.id.delete);
        adapter = new DeleteAdapter(getApplication(), search, listview,delete);
       }

DeleteAdapter

public class DeleteAdapter extends BaseAdapter {

    private static ArrayList<SearchList> search;
    private LayoutInflater mInflater;
    ListView listview;
    Button delete;
    private static int checkBoxCounter = 0;

    public DeleteAdapter(Context context, ArrayList<SearchList> searchList, ListView listview,Button delete) {
        search=searchList;
        delete=delete;
        this.listview=listview;
        mInflater = LayoutInflater.from(context);

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

    public SearchList getItem(int position) {
        return search.get(position);
    }

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

    public void removeItem(int position) {
        search.remove(position);
        this.notifyDataSetChanged();
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        //delete =(Button)convertView.findViewById(R.id.delete);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.delete_task_with_edittext, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.task_title);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.date = (TextView) convertView.findViewById(R.id.date);
            holder.ckbox = (CheckBox) convertView.findViewById(R.id.checkBox);
            holder.ckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(buttonView.isChecked()) {
                        int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                        search.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                        checkBoxCounter ++;
                    }
                    else
                    {
                        checkBoxCounter--;
                    }
                    delete.setText(checkBoxCounter+"");
                }

            });
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.ckbox.setTag(position);
        holder.text.setText(search.get(position).getTitle());
        holder.time.setText(search.get(position).getTime());
        holder.date.setText(search.get(position).getDate());
        holder.ckbox.setChecked(search.get(position).isSelected());
        return convertView;

    }

    static class ViewHolder {
        TextView text,time,date;
        CheckBox ckbox;

    }
}

当我单击复选框时,我将在行delete.setText(checkBoxCounter+"");中收到nullPointerException错误,但我已在DeleteTask中声明它。任何人都知道如何解决它?

完整StackTrace

11-21 01:12:24.005    5695-5695/com.example.seng.healthyapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.seng.healthyapp.adapter.DeleteAdapter$1.onCheckedChanged(DeleteAdapter.java:90)
            at android.widget.CompoundButton.setChecked(CompoundButton.java:126)
            at android.widget.CompoundButton.toggle(CompoundButton.java:87)
            at android.widget.CompoundButton.performClick(CompoundButton.java:99)
            at android.view.View$PerformClick.run(View.java:17660)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5433)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:924)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
            at dalvik.system.NativeStart.main(Native Method)

我遇到了另一个问题。当我单击前三个复选框时,按钮setText显示3.但是当我向下滚动时,计数器将减少。

2 个答案:

答案 0 :(得分:1)

更新适配器中的代码

 public DeleteAdapter(Context context, ArrayList<SearchList> searchList, ListView listview,Button delete) {
    search=searchList;
    this.delete=delete;
    this.listview=listview;
    mInflater = LayoutInflater.from(context);

}

答案 1 :(得分:0)

在适配器的构造函数中:

public DeleteAdapter(Context context, ArrayList<SearchList> searchList, ListView listview,Button delete) {
    search=searchList;
    delete=delete;
    this.listview=listview;
    mInflater = LayoutInflater.from(context);

}

您要将delete分配给delete,它不会初始化您的实例变量delete。这给它一个默认值null,当由于调用setText()方法而点击复选框时会导致NullPointerException。