ListView滚动上的复选框重置

时间:2018-07-16 18:23:46

标签: java android

我的问题是,当我在列表视图中滚动时,信息会更新,导致复选框被重置。我需要能够调用allChecked()函数来查看是否已选中所有复选框,但是由于状态被重置,所以我无法执行此操作。如何使状态保持不变?

这是我用于处理列表视图的自定义地址

public class CustomAdapter extends ArrayAdapter {

private ArrayList<DataModel> dataSet;
Context mContext;

// View lookup cache
private static class ViewHolder {
    TextView txtName;
    CheckBox checkBox;
}

public CustomAdapter(ArrayList data, Context context) {
    super(context, R.layout.row_item, data);
    this.dataSet = data;
    this.mContext = context;

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

@Override
public DataModel getItem(int position){
    return dataSet.get(position);
}


@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {

    ViewHolder viewHolder;
    View result;

    if (convertView == null) {
        viewHolder = new ViewHolder();
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);
        viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);
        viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);


        result=convertView;
        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
        result=convertView;
    }
    DataModel item = getItem(position);

    viewHolder.txtName.setText(item.toDo);
    viewHolder.checkBox.setChecked(item.checked);


    return result;
 }
}

这是我用于存储列表文本以及是否选中列表文本的数据模型

public class DataModel {
public String toDo;
boolean checked;

DataModel(String toDo, boolean checked){
    this.toDo = toDo;
    this.checked = checked;
 }
}

列表的xml文件以防万一

   <TextView
    android:id="@+id/txtName"
    android:layout_width="402dp"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:padding="30dp"
    android:textColor="@color/GbzBlack"
    android:textSize="24sp" />

<CheckBox
    android:id="@+id/checkBox"
    style="@style/Widget.AppCompat.CompoundButton.CheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_centerVertical="true" />

最后是我如何在主体内部实现列表视图

   if (formTitle == "215CANNA") {
        Spinner tmpSpinner = findViewById(R.id.bayNameSpinner5);
        TextView tmpTextView = findViewById(R.id.bayNamesText5);
        tmpSpinner.setVisibility(View.INVISIBLE);
        tmpTextView.setVisibility(View.INVISIBLE);

        //bay 1
        myAdapter = new CustomAdapter(cannaGinB1, getApplicationContext());
        myListView.setAdapter(myAdapter);

        if (allChecked) {
            nextAndExportBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    buttonCounter++;
                    //bay 2
                    if (buttonCounter == 1) {
                        myAdapter = new CustomAdapter(cannaGinB2, getApplicationContext());
                        myListView.setAdapter(myAdapter);
                    }
                    //bay 3
                    if (buttonCounter == 2) {
                        myAdapter = new CustomAdapter(cannaGinB3, getApplicationContext());
                        myListView.setAdapter(myAdapter);
                    }
                    //bay 4
                    if (buttonCounter == 3) {
                        myAdapter = new CustomAdapter(cannaGinB4, getApplicationContext());
                        myListView.setAdapter(myAdapter);
                        nextAndExportBtn.setText("Export");
                    }

                }
            });
        }

        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                myAdapter.notifyDataSetChanged();
                totalChecked++;
                int listSize = myListView.getCount();
                if (listSize == totalChecked){
                    allChecked = true;
                }
            }
        });

更新

更改

  viewHolder.checkBox.setChecked(item.checked)

 viewHolder.checkBox.setTag(item.checked)

使它保持选中状态,但是,选中第一个复选框后,最后一个复选框也会被选中

2 个答案:

答案 0 :(得分:1)

创建一个可以在列表视图中为每种情况保留一个布尔值的arraylist。选中此框后,请为每个结果保存一个布尔值,说明是否已选中。这样,当您在创建每个视图之前创建列表视图时,请检查该视图布尔值是否为true或false并将该框设置为选中/未选中。

答案 1 :(得分:0)

谢谢您的想法,主要解决方法是将Nigel Brown所说的内容与数组结合在一起,并在类内部而不是外部创建onclicklistner