I used a ExpandableListView , in group view in layout I have a textView and checkbox , but when I setSelected a checkbox doesn't remeber this , when I changed state a checkbox I save in realm , but when I open my activity doesn't set select on true checkbox. This is my adapter :
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
public static ArrayList<Integer> selected = new ArrayList<>();
private Realm realm = Realm.getDefaultInstance();
public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.expandedListItem);
expandedListTextView.setText(expandedListText);
return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
@Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
@Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public View getGroupView(final int listPosition, boolean isExpanded, View convertView, ViewGroup parent) {
SharedPreferences pref = context.getSharedPreferences("MAIN_PREF", MODE_PRIVATE);
final SharedPreferences.Editor editor = pref.edit();
final String listTitle = (String) getGroup(listPosition);
final boolean[] sel = {RealmController.getGroup(listTitle, realm).isSelect()};
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// convertView = layoutInflater.inflate(R.layout.list_group, null);
convertView = layoutInflater.inflate(R.layout.holder_layout, null);
}
// TextView listTitleTextView = (TextView) convertView
// .findViewById(R.id.listTitle);
TextView listTitleTextView = (TextView) convertView.findViewById(R.id.showText);
final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxHolder);
// if (pref.getInt(listTitle, 0) == 1) {
// checkBox.setSelected(true);
// } else {
// checkBox.setSelected(false);
// }
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkBox.isSelected()) {
checkBox.setSelected(false);
RealmResults<ObjectsInGroupRealm> objectsInGroupRealms = RealmController.getObjects(realm, listTitle);
int position = -1;
for (ObjectDefExtends objectDefExtends : Singleton.getInstance().getListaODE()) {
for (ObjectsInGroupRealm o : objectsInGroupRealms) {
if (o.getName().equals(objectDefExtends.name)) {
position = Singleton.getInstance().getListaODE().indexOf(objectDefExtends);
Singleton.getInstance().getListaODE().get(position).visible = false;
editor.putInt(Singleton.getInstance().getListaODE().get(position).id.toString(), 0);
editor.apply();
// RealmController.updateGroup(o.getName(),false,realm);
}
}
}
// editor.putInt(listTitle, 0);
// editor.apply();
selected.remove(Integer.valueOf(listPosition));
} else {
checkBox.setSelected(true);
selected.add(listPosition);
RealmResults<ObjectsInGroupRealm> objectsInGroupRealms = RealmController.getObjects(realm, listTitle);
int position = -1;
for (ObjectDefExtends objectDefExtends : Singleton.getInstance().getListaODE()) {
for (ObjectsInGroupRealm o : objectsInGroupRealms) {
if (o.getName().equals(objectDefExtends.name)) {
position = Singleton.getInstance().getListaODE().indexOf(objectDefExtends);
Singleton.getInstance().getListaODE().get(position).visible = false;
editor.putInt(Singleton.getInstance().getListaODE().get(position).id.toString(), 1);
editor.apply();
RealmController.updateGroup(o.getName(),true,realm);
sel[0] =true;
checkBox.setSelected(true);
// notifyDataSetChanged();
}
}
}
// editor.putInt(listTitle, 1);
// editor.apply();
}
// notifyDataSetChanged();
}
});
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
checkBox.setSelected(sel[0]);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
Aucun commentaire:
Enregistrer un commentaire