向下滚动列表视图时,一些复选框变为false状态。
我已经厌倦了几个解决方案,但我很困惑
我想要一些简单的代码,因为我不喜欢从零构建所有代码。
注意:每行有三个复选框,以便我使用三个列表。
public class StudentAdapter extends ArrayAdapter<StudentHelper> {
final String NAMESPACE = "http://ws.sams.com";
String URL = "http://sams-app.com:8080/webService/services/InsertDataWS?WSDL";
final String METHOD_NAME = "insertApsentData";
final String SOAP_ACTION = "http://ws.sams.com/insertApsentData";
private final List<StudentHelper> StudentNamesList; // for the student names
// .
private final List<StudentHelper> StudentIdsList; // for the student ids .
private final Activity activity;
Context context;
boolean test = false;
public StudentAdapter(Activity activity,
List<StudentHelper> StudentNamesList,
List<StudentHelper> StudentIdsList, Context context,
Activity listActivity1, boolean test) {
super(activity, R.layout.activity_list, StudentNamesList);
this.activity = listActivity1;
this.StudentNamesList = StudentNamesList;
this.context = context;
this.StudentIdsList = StudentIdsList;
this.test = test;
// this.filter = new InteractiveArrayAdapter();
}
@Override
public StudentHelper getItem(int position) {
return super.getItem(position);
}
class ViewHolder {
protected TextView StudentName;
protected TextView StudentId;
protected TextView Course_Name;
protected TextView Class_id;
protected CheckBox checkbox;
protected CheckBox checkbox1;
protected CheckBox checkbox2;
protected TableRow row;
}
// List<Integer> checkBoxState = new ArrayList<Integer>();
// TreeSet<Integer> checkBoxState = new TreeSet<Integer>();
HashMap<Integer, Integer> checkBoxState = new HashMap<Integer, Integer>();
final List<Boolean> SparseBooleanArray = null;
final List<Boolean> SparseBooleanArray1 = null;
final List<Boolean> SparseBooleanArray2 = null;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = activity.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.Course_Name = (TextView) view
.findViewById(R.id.textView3);
viewHolder.Class_id = (TextView) view.findViewById(R.id.textView2);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.CheckBox);
viewHolder.checkbox1 = (CheckBox) view
.findViewById(R.id.CheckBox01);
viewHolder.checkbox2 = (CheckBox) view
.findViewById(R.id.CheckBox02);
viewHolder.row = (TableRow) view.findViewById(R.id.TableRow05);
Typeface font = Typeface.createFromAsset(activity.getAssets(),
"extrafine.ttf");
Typeface fontar = Typeface.createFromAsset(activity.getAssets(),
"extrafinear.ttf");
viewHolder.StudentName.setTypeface(fontar);
viewHolder.StudentId.setTypeface(font);
view.setTag(viewHolder);
viewHolder.row.setTag(StudentNamesList.get(position));
viewHolder.checkbox.setTag(StudentNamesList.get(position));
viewHolder.checkbox1.setTag(StudentNamesList.get(position));
viewHolder.checkbox2.setTag(StudentNamesList.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(StudentNamesList
.get(position));
((ViewHolder) view.getTag()).checkbox1.setTag(StudentNamesList
.get(position));
((ViewHolder) view.getTag()).checkbox2.setTag(StudentNamesList
.get(position));
((ViewHolder) view.getTag()).row.setTag(StudentNamesList
.get(position));
}
Button sumbit = (Button) activity.findViewById(R.id.sumbitbtn);
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.checkbox1
.setChecked(StudentNamesList.get(position).isSelected());
holder.checkbox2
.setChecked(StudentNamesList.get(position).isSelected());
holder.checkbox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
SparseBooleanArray.add(position, true);
holder.checkbox.setSelected(true);
holder.checkbox1.setChecked(false);
holder.checkbox2.setChecked(false);
holder.row.setBackgroundResource(R.drawable.green);
ViewGroup row1 = (ViewGroup) holder.checkbox
.getParent();
View view1 = ((ViewGroup) row1).getChildAt(4);
if (view1 instanceof TextView) {
holder.StudentId = (TextView) view1;
checkBoxState.put(Integer
.parseInt(holder.StudentId.getText()
.toString()), 0);
}
} else if (holder.checkbox.isChecked() == false) {
holder.row.setBackgroundResource(R.drawable.rep);
holder.checkbox.setSelected(false);
ViewGroup row1 = (ViewGroup) holder.checkbox
.getParent();
View view1 = ((ViewGroup) row1).getChildAt(4);
if (view1 instanceof TextView) {
holder.StudentId = (TextView) view1;
checkBoxState.remove(Integer
.parseInt(holder.StudentId.getText()
.toString()));
}
}
}
});
holder.checkbox1
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
SparseBooleanArray1.add(position, true);
holder.checkbox1.setSelected(true);
holder.checkbox.setChecked(false);
holder.checkbox2.setChecked(false);
ViewGroup row1 = (ViewGroup) holder.checkbox1
.getParent();
View view1 = ((ViewGroup) row1).getChildAt(4);
if (view1 instanceof TextView) {
holder.StudentId = (TextView) view1;
checkBoxState.put(Integer
.parseInt(holder.StudentId.getText()
.toString()), 1);
holder.row
.setBackgroundResource(R.drawable.red);
}
} else if (holder.checkbox1.isChecked() == false) {
holder.row.setBackgroundResource(R.drawable.rep);
holder.checkbox1.setSelected(false);
ViewGroup row1 = (ViewGroup) holder.checkbox1
.getParent();
View view1 = ((ViewGroup) row1).getChildAt(4);
if (view1 instanceof TextView) {
holder.StudentId = (TextView) view1;
checkBoxState.remove(Integer
.parseInt(holder.StudentId.getText()
.toString()));
}
}
}
});
holder.checkbox2
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
SparseBooleanArray2.add(position, true);
holder.checkbox2.setSelected(true);
holder.checkbox1.setChecked(false);
holder.checkbox.setChecked(false);
ViewGroup row1 = (ViewGroup) holder.checkbox1
.getParent();
View view1 = ((ViewGroup) row1).getChildAt(4);
if (view1 instanceof TextView) {
holder.StudentId = (TextView) view1;
checkBoxState.put(Integer
.parseInt(holder.StudentId.getText()
.toString()), 2);
holder.row
.setBackgroundResource(R.drawable.blue);
}
} else if (holder.checkbox2.isChecked() == false) {
holder.row.setBackgroundResource(R.drawable.rep);
holder.checkbox2.setSelected(false);
ViewGroup row1 = (ViewGroup) holder.checkbox2
.getParent();
View view1 = ((ViewGroup) row1).getChildAt(4);
if (view1 instanceof TextView) {
holder.StudentId = (TextView) view1;
checkBoxState.remove(Integer
.parseInt(holder.StudentId.getText()
.toString()));
}
}
}
});
/*
* if(test){
*
*
* for (Map.Entry<Integer, Integer> mapEntry : checkBoxState
* .entrySet()){
*
* checkBoxState.remove(mapEntry.getKey());
*
* }
*
*
* }
*/
holder.checkbox.setChecked(SparseBooleanArray.get(position));
// holder.row.setBackgroundResource(R.drawable.green);
holder.checkbox1.setChecked(SparseBooleanArray1.get(position));
// holder.row.setBackgroundResource(R.drawable.red);
holder.checkbox2.setChecked(SparseBooleanArray2.get(position));
// holder.row.setBackgroundResource(R.drawable.blue);
sumbit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (checkBoxState.isEmpty()) {
Toast.makeText(activity, "OOOh it's empaty ",
Toast.LENGTH_SHORT).show();
}
else {
ProgressDialog progressDialog = new ProgressDialog(activity);
progressDialog
.setMessage("جاري ارسال البيانات ، الرجاء الانتظار");
progressDialog.show();
SendListSync asynTask = new SendListSync(context, activity,
checkBoxState, progressDialog);
asynTask.execute();
}
}
});
return view;
}
}