I have an Expandablelistview that I load from database some data and I put that data in with checkboxes, but the problem is, if I check something on group 1 and open group 0, the checked checkbox at group 1 become unchecked and the last checkbox at group 0 become checked.
My adapter:
public class ExpandableListCheckboxAdapter extends BaseExpandableListAdapter {
private List<String> listFather;
private HashMap<String, ArrayList<CheckBox>> listChildren;
private LayoutInflater inflater;
private SparseBooleanArray spa = new SparseBooleanArray();
public ExpandableListCheckboxAdapter(Context context, List<String> listGroup, HashMap<String, ArrayList<CheckBox>> listData){
this.listFather = listGroup;
this.listChildren = listData;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getGroupCount() {
return listFather.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return listChildren.get(listFather.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return listFather.get(groupPosition);
}
public String getGroupText(int groupPosition) {
return listFather.get(groupPosition).toString();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return listChildren.get(listFather.get(groupPosition)).get(childPosition);
}
public String getChildText(int groupPosition, int childPosition) {
return listChildren.get(listFather.get(groupPosition)).get(childPosition).getText().toString();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
public boolean isChecked(int groupPosition, int childPosition) {
return listChildren.get(listFather.get(groupPosition)).get(childPosition).isChecked();
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolderGroup holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.expandable_father, null);
holder = new ViewHolderGroup();
convertView.setTag(holder);
holder.txvFatherList = (TextView) convertView.findViewById(R.id.txvFatherList);
}
else{
holder = (ViewHolderGroup) convertView.getTag();
}
holder.txvFatherList.setText(listFather.get(groupPosition));
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
String val = (String) getChildText(groupPosition, childPosition);
final ExpandableListCheckboxAdapter.ViewHolderItem holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.expandable_children_subject, null);
holder = new ExpandableListCheckboxAdapter.ViewHolderItem();
convertView.setTag(holder);
holder.ckbChildrenSub = (CheckBox) convertView.findViewById(R.id.ckbChildrenSub);
holder.ckbChildrenSub.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(holder.ckbChildrenSub.isChecked()){
if(universalVariables.subjects.containsKey(getGroupText(groupPosition))){
List<String> aux = new ArrayList<>();
aux = universalVariables.subjects.get(getGroupText(groupPosition));
if(!aux.contains(holder.ckbChildrenSub.getText().toString()))
aux.add(holder.ckbChildrenSub.getText().toString());
universalVariables.subjects.put(getGroupText(groupPosition), aux);
}
else{
List<String> aux = new ArrayList<>();
aux.add(holder.ckbChildrenSub.getText().toString());
universalVariables.subjects.put(getGroupText(groupPosition), aux);
}
}
else {
if (universalVariables.subjects.containsKey(getGroupText(groupPosition))) {
List<String> aux = new ArrayList<>();
aux = universalVariables.subjects.get(getGroupText(groupPosition));
if (aux.contains(getChildText(groupPosition, childPosition))) {
aux.remove(getChildText(groupPosition, childPosition));
universalVariables.subjects.put(getGroupText(groupPosition), aux);
}
}
}
}
});
}
else{
holder = (ExpandableListCheckboxAdapter.ViewHolderItem) convertView.getTag();
}
holder.ckbChildrenSub.setText(val);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
class ViewHolderGroup {
TextView txvFatherList;
}
class ViewHolderItem {
CheckBox ckbChildrenSub;
}
}
Method to load data from database:
private void loadSubjects(){
//Use database
for (int i = 0; i < disciplinas.size(); i++) {
if (!listGroup.contains(disciplinas.get(i)))
listGroup.add(disciplinas.get(i));
int idGeneric = 0;
ArrayList<CheckBox> auxList = new ArrayList<CheckBox>();
databaseHelper = dbHelper.getInstance(this, DB_NAME);
String generic = disciplinas.get(i);
String query = "SELECT Assunto FROM Assunto WHERE Disciplina='" + generic + "'";
Cursor c1 = databaseHelper.rawQuery(query);
if (c1 != null && c1.getCount() != 0) {
if (c1.moveToFirst()) {
do {
String assunto = c1.getString(c1.getColumnIndex("Assunto"));
checkbox = new CheckBox(this);
checkbox.setText(assunto);
auxList.add(checkbox);
idGeneric = idGeneric + 1;
} while (c1.moveToNext());
}
}
if (c1 != null)
c1.close();
listData.put(disciplinas.get(i), auxList);
}
expCheckListView = (ExpandableListView) findViewById(R.id.elstvSub);
checkAdapter = new ExpandableListCheckboxAdapter(Assuntos.this, listGroup, listData);
expCheckListView.setAdapter(checkAdapter);
}
Children XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/ckbChildrenSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Father XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txvFatherList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="18dp"
android:layout_marginLeft="33dp" />
</LinearLayout>
Aucun commentaire:
Enregistrer un commentaire