我有一个对话框,其中有一个listView of checkbox和textview。我在textView中显示了stName Arraylist。现在我想要的是在我重新打开对话框后选中复选框时应该显示检查的checboxes。
public class StudentNameAdapter extends ArrayAdapter{
Context context;
BaseActivity activity;
ArrayList<String> updatedList,studentId,studentIdUpdated;
ArrayList<CheckModelClass> stName;
public StudentNameAdapter(Context context, BaseActivity activity, ArrayList<CheckModelClass> stName, ArrayList<String> updatedList, ArrayList<String> studentId, ArrayList<String> studentIdUpdated) {
super(context, R.layout.student_name_listview,stName);
this.context = context;
this.stName = stName;
this.activity =activity;
this.updatedList =updatedList;
this.studentId = studentId;
this.studentIdUpdated = studentIdUpdated;
}
private static class ViewHolder {
CheckBox checkbox;
TextView text;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.student_name_listview, parent, false);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.text1);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
stName.get(getPosition).setSelected(buttonView.isChecked());
if(isChecked){
if(studentIdUpdated.contains(studentId.get(getPosition))){
}
else{
updatedList.add(stName.get(getPosition).getName());
studentIdUpdated.add(studentId.get(getPosition));
}
}else
{
updatedList.remove(stName.get(getPosition).getName());
studentIdUpdated.remove(studentId.get(getPosition));
}
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.text1, viewHolder.text);
convertView.setTag(R.id.checkbox, viewHolder.checkbox);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position);
viewHolder.text.setText(stName.get(position).getName());
viewHolder.checkbox.setChecked(stName.get(position).isSelected());
return convertView;
}
}
答案 0 :(得分:0)
试试这个..
viewHolder.checkbox.setChecked(stName.get(position).isSelected());
viewHolder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox cb = (CheckBox) view;
if (cb.isChecked()) {
if(studentIdUpdated.contains(studentId.get(getPosition))){
}
else{
updatedList.add(stName.get(getPosition).getName());
studentIdUpdated.add(studentId.get(getPosition));
}
}else
{
updatedList.remove(stName.get(getPosition).getName());
studentIdUpdated.remove(studentId.get(getPosition));
}
}
});