-1
我有一個對話框,其中有一個複選框和textview的listView。我在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;
}
}
這是不行的。你的回答與我的問題沒有任何關係。我想要的是一旦我選擇任何複選框並關閉對話框。然後當我重新打開dailog時,應該看到檢查。 –
你已經使用checkedChangeListner這將無法與視圖的完美工作。由於viewholder一次最多可以存儲10行,所以當你滾動時,checkdChangeListner會將值更改爲false。這就是爲什麼你可以使用onClickListner,因爲onClickListner只有在你點擊它時纔會工作 – kkk