gpt4 book ai didi

android - 复选框会在 Android 中自动选中

转载 作者:行者123 更新时间:2023-11-30 03:23:49 29 4
gpt4 key购买 nike

我有一个 ListView ,其中有几行由 inflator 创建,每行有两个 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;

}

最佳答案

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

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);
}
}
});

关于android - 复选框会在 Android 中自动选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18613958/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com