复选框会在Android中自动检查

时间:2013-09-04 12:34:31

标签: android listview android-listview layout-inflater

我有一个列表视图,其中有几行由充气器创建,每行有两个TextView和三个复选框。一切正常,但是当我用手指向下或向上滚动时,我注意到一些复选框会自动检查。我不知道为什么它在使用滚动时会这样做。

我的代码:

  public class InteractiveArrayAdapter extends ArrayAdapter<Student> implements
    Filterable {
final String NAMESPACE = "http://ws.sams.com";
String URL = "http://88.198.82.92:8080/sams1/services/InsertDataWS?WSDL"; // usint
                                                                            // localhost
final String METHOD_NAME = "insertApsentData";
final String SOAP_ACTION = "http://ws.sams.com/insertApsentData";

List<Integer> checkBoxState = new ArrayList<Integer>(200);
// List<Integer> checkBoxState2 = new ArrayList<Integer>(200);
// List<Integer> checkBoxState3 = new ArrayList<Integer>(200);
// Set<Integer> numbers = new HashSet<Integer>();

private final List<Student> StudentNamesList; // for the student names .
private final List<Student> StudentIdsList; // for the student ids .
private final Activity context;

public InteractiveArrayAdapter(Activity context,
        List<Student> StudentNamesList, List<Student> StudentIdsList) {
    super(context, R.layout.activity_list, StudentNamesList);
    this.context = context;
    this.StudentNamesList = StudentNamesList;
    this.StudentIdsList = StudentIdsList;
}

protected Button sumbit;

class ViewHolder {
    protected TextView StudentName;
    protected TextView StudentId;
    protected CheckBox checkbox;
    protected CheckBox checkbox2;
    protected CheckBox checkbox3;
    protected TableRow row;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.rep, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.StudentName = (TextView) view
                .findViewById(R.id.TextView07);
        viewHolder.StudentId = (TextView) view
                .findViewById(R.id.TextView08);
        viewHolder.checkbox = (CheckBox) view.findViewById(R.id.CheckBox05);
        viewHolder.checkbox2 = (CheckBox) view
                .findViewById(R.id.CheckBox02);
        viewHolder.checkbox3 = (CheckBox) view
                .findViewById(R.id.CheckBox01);
        viewHolder.row = (TableRow) view.findViewById(R.id.TableRow05);
        // final EditText searchEdit = (EditText)
        // view.findViewById(R.id.searchlable);
        Typeface font = Typeface.createFromAsset(context.getAssets(),
                "extrafine.ttf");
        Typeface fontar = Typeface.createFromAsset(context.getAssets(),
                "extrafinear.ttf");
        viewHolder.StudentName.setTypeface(fontar);
        viewHolder.StudentId.setTypeface(font);

        sumbit = (Button) context.findViewById(R.id.sumbitbtn);

        view.setTag(viewHolder);
        viewHolder.row.setTag(StudentNamesList.get(position)); // here note
        viewHolder.checkbox.setTag(StudentNamesList.get(position));
        viewHolder.checkbox2.setTag(StudentNamesList.get(position));
        viewHolder.checkbox3.setTag(StudentNamesList.get(position));
    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).checkbox.setTag(StudentNamesList
                .get(position));
        ((ViewHolder) view.getTag()).checkbox2.setTag(StudentNamesList
                .get(position));
        ((ViewHolder) view.getTag()).checkbox3.setTag(StudentNamesList
                .get(position));
        ((ViewHolder) view.getTag()).row.setTag(StudentNamesList
                .get(position));
    }

    final ViewHolder holder = (ViewHolder) view.getTag();
    holder.StudentName.setText(StudentNamesList.get(position).getName());
    holder.StudentId.setText(StudentIdsList.get(position).getName());
    holder.checkbox.setChecked(StudentNamesList.get(position).isSelected());
    holder.checkbox2
            .setChecked(StudentNamesList.get(position).isSelected());
    holder.checkbox3
            .setChecked(StudentNamesList.get(position).isSelected());

    holder.checkbox
            .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {

                    if (isChecked) {
                        holder.checkbox2.setChecked(false);
                        holder.checkbox3.setChecked(false);
                        holder.row.setBackgroundResource(R.drawable.green);
                    } else {

                    }

                    ((Student) holder.checkbox.getTag()).setSelected(true);

                    if (((CheckBox) holder.checkbox).isChecked()) {
                        ViewGroup row1 = (ViewGroup) holder.checkbox
                                .getParent();

                        View view1 = ((ViewGroup) row1).getChildAt(4);
                        if (view1 instanceof TextView) {

                            holder.StudentName = (TextView) view1;

                            checkBoxState.add(Integer
                                    .parseInt(holder.StudentName.getText()
                                            .toString()));
                        }

                    }

                }
            });

    holder.checkbox3
            .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {

                    if (isChecked) {
                        holder.row.setBackgroundResource(R.drawable.red);
                        holder.checkbox2.setChecked(false);
                        holder.checkbox.setChecked(false);
                    } else {

                    }

                    ((Student) holder.checkbox3.getTag()).setSelected(true);

                    // Toast.makeText(context,
                    // holder.text.getText().toString() ,
                    // Toast.LENGTH_SHORT).show();
                    if (((CheckBox) holder.checkbox3).isChecked()) {
                        ViewGroup row1 = (ViewGroup) holder.checkbox3
                                .getParent();

                        // for (int itemPos = 1; itemPos < ((ViewGroup)
                        // row1).getChildCount(); itemPos++) {

                        View view1 = ((ViewGroup) row1).getChildAt(4);
                        if (view1 instanceof TextView) {

                            holder.StudentName = (TextView) view1;
                            // numbers.add(Integer.parseInt(holder.StudentName.getText().toString()));}

                            checkBoxState.add(Integer
                                    .parseInt(holder.StudentName.getText()
                                            .toString()));
                        }
                        // else{
                        // checkBoxState.add(1);}

                    }

                }
            });

    holder.checkbox2
            .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {

                    if (isChecked) {
                        holder.row.setBackgroundResource(R.drawable.blue);
                        holder.checkbox.setChecked(false);
                        holder.checkbox3.setChecked(false);

                    } else {

                    }

                    ((Student) holder.checkbox2.getTag()).setSelected(true);

                    // Toast.makeText(context,
                    // holder.text.getText().toString() ,
                    // Toast.LENGTH_SHORT).show();
                    if (((CheckBox) holder.checkbox2).isChecked()) {
                        ViewGroup row1 = (ViewGroup) holder.checkbox2
                                .getParent();

                        // for (int itemPos = 1; itemPos < ((ViewGroup)
                        // row1).getChildCount(); itemPos++) {

                        View view1 = ((ViewGroup) row1).getChildAt(4);
                        if (view1 instanceof TextView) {

                            holder.StudentName = (TextView) view1;

                            // numbers.add(Integer.parseInt(holder.StudentName.getText().toString()));}
                            checkBoxState.add(Integer
                                    .parseInt(holder.StudentName.getText()
                                            .toString()));
                        }
                        // else{
                        // checkBoxState.add(1);}

                    }

                }
            });

    sumbit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            for (int i = 0; i < checkBoxState.size(); i++) {

                Toast.makeText(context, checkBoxState.get(i).toString(),
                        Toast.LENGTH_SHORT).show();

                // For webservice propose

                try {

                    SoapObject request = new SoapObject(NAMESPACE,
                            METHOD_NAME);
                    PropertyInfo pi = new PropertyInfo();
                    pi.setName("SID");
                    pi.setValue(Integer.parseInt(checkBoxState.get(i)
                            .toString()));
                    pi.setType(Integer.class);

                    SharedPreferences mPrefs = context
                            .getSharedPreferences("ITeacher", 0);
                    // int str1 = mPrefs.getInt("1",0);
                    int str1 = 1;
                    PropertyInfo pi2 = new PropertyInfo();
                    pi2.setName("TID");
                    pi2.setValue(str1);
                    pi2.setType(Integer.class);

                    PropertyInfo pi3 = new PropertyInfo();
                    pi3.setName("class_id");
                    pi3.setValue(str1);
                    pi3.setType(Integer.class);

                    request.addProperty(pi);
                    request.addProperty(pi2);
                    request.addProperty(pi3);
                    final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                            SoapEnvelope.VER11);
                    envelope.setOutputSoapObject(request);
                    final HttpTransportSE androidHttpTransport = new HttpTransportSE(
                            URL);
                    androidHttpTransport.call(SOAP_ACTION, envelope);

                }

                catch (Exception e) {
                }
            }
        }

    });

    return view;

}

}

3 个答案:

答案 0 :(得分:1)

如果您在开始之前知道要检查或取消选中的项目,那么只需将其设置为您正在使用的适配器

if (checkBoxState.contains(categories.get(position).categoryid)) {
            holder.checkbox.setChecked(true);
} else {
            holder.checkbox.setChecked(false);
}

然后继续这样的代码

holder.checkbox
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        if (isChecked) {
                             checkBoxState.add(categories.get(position).categoryid);
                            } else {
                             checkBoxState.remove(categories.get(position).categoryid);
                            }
                        }
                    });

答案 1 :(得分:0)

这是因为视图得到回收。当您滚动ListView时,将重复使用屏幕外的视图以避免再次膨胀它们。

您可以确保每次调用适配器的getView()方法时都会正确更新复选框。否则,如果在上次使用视图时检查它们,它们将保持检查状态。

答案 2 :(得分:0)

ListView重用行来创建新的(因此getView中的convertView参数)。在您的代码中,如果您获得非null的convertView,则必须将其复选框选中状态更改为与新内容相关的状态。

由于正在重新绘制行,例如,第1行中复选框的选中状态将被复制到从第1行复制的行;从而将新行的CheckBox设置为选中。

解决问题的工作实现是使用布尔值数组来存储每个复选框的当前状态。

boolean[] itemChecked;

然后在getView中,在复选框上设置一个监听器(必须为此设置最终位置)。您还可以使用数组设置检查状态。

    cb.setOnCheckedChangeListener (new OnCheckedChangeListener () {
    public void onCheckedChanged (CompoundButton btn, boolean isChecked) {
    itemChecked[position] = isChecked;
    }
});

    cb.setChecked(itemChecked[position]);

但是,如果你的列表中有大量的元素,那么使用数组会被认为效率低下。