2

I am developing an app that use to ExpandableListview. Firstly I am adding headers and then I am adding childs to specific headers. But when I add childs , every header of ExpandableListView updating. (Screenshots explaining)

I use BaseExpandableListView adapter but not afford to solve this issue. Any help very worthy for me. Thank you

Here my Adapter Class;

public class SYCriteraAdapter extends BaseExpandableListAdapter {

private LayoutInflater inflater;
private ArrayList<SYCriteraModel> mParent;
private ArrayList<ArrayList<SYCriteraModel>> mChild;
private View view;


public ArrayList<SYCriteraModel> getMParent() {
    return mParent;
}


public SYCriteraAdapter(Context context,ArrayList<SYCriteraModel> parentList) {
    this.mParent = parentList;
    this.inflater = LayoutInflater.from(context);

}

public SYCriteraAdapter(Context context,ArrayList<SYCriteraModel> parentList,ArrayList<ArrayList<SYCriteraModel>> childList) {
    this.mParent = parentList;
    this.inflater = LayoutInflater.from(context);
    this.mChild = childList;

}

// counts the number of group/parent items so the list knows how many
// times calls getGroupView() method
public int getGroupCount() {
    return mParent.size();
}

// counts the number of children items so the list knows how many times
// calls getChildView() method
public int getChildrenCount(int parentPosition) {
    int childCount = 0;
    try {
        childCount = mChild.get(parentPosition).size();
    }
    catch (Exception e){
        System.out.println("Exception " + e);
    }
    return childCount;
}

// gets the title of each parent/group
public Object getGroup(int i) {
    return null;
}

// gets the name of each item
public Object getChild(int parentPosition, int childPosition) {

    return null;
}

public long getGroupId(int parentPosition) {
    return 0;
}

public long getChildId(int i, int childPosition) {
    return 0;
}

public boolean hasStableIds() {
    return false;
}

// in this method you must set the text to see the parent/group on the list

public View getGroupView(int parentPosition, boolean b, View view, ViewGroup viewGroup) {
    if (view == null) {
        view = inflater.inflate(R.layout.critera_upper_adapter, viewGroup, false);
    }
    TextView uppercritera = (TextView) view.findViewById(R.id.etCritera);
    this.notifyDataSetChanged();
    uppercritera.setText(mParent.get(parentPosition).getParent());
    return view;

}

// in this method you must set the text to see the children on the list

public View getChildView(int parentPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
    if (view == null) {
        view = inflater.inflate(R.layout.critera_lower_adapter, viewGroup, false);
    }
    TextView lowerCritera = (TextView) view.findViewById(R.id.etCritera);
    lowerCritera.setText(mChild.get(parentPosition).get(childPosition).getChildren());

    return view;
}

public boolean isChildSelectable(int i, int i1) {
    return true;
}


}

enter image description here enter image description here

salih
  • 727
  • 13
  • 36

2 Answers2

1

There is one method getChildView inside your BaseExpandable Adapter check parent position in that method .

public View getChildView(int parentPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
    view = inflater.inflate(R.layout.critera_lower_adapter, viewGroup, false);
}

//Here You have to check position of parentPosition child should have added or not i,e

if(parentPosition==1 || parentPosition==3 || parentPosition==5)

    TextView lowerCritera = (TextView) view.findViewById(R.id.etCritera);
lowerCritera.setText(mChild.get(parentPosition).get(childPosition).getChildren());

return view;
Ajay Pandya
  • 2,417
  • 4
  • 29
  • 65
  • But I dont know parent position sometimes. Assume that I defined parent position 5 and I only have 3 parent ? How can I gain flexibility to this piece of code ? – salih Feb 29 '16 at 07:50
  • if you dont know the position of parent than how specifically set the child? ok don't be confused check the parent i mean header text if its match than only add the child. – Ajay Pandya Feb 29 '16 at 07:53
  • I know parent position but It changable. I refer its changability. – salih Feb 29 '16 at 07:56
  • Not sure but for reference you can try this http://stackoverflow.com/a/15348971/3514144 – Ajay Pandya Feb 29 '16 at 08:04
  • By default your parentview is showing right? so you can set logic as like when parent in your case header is clicked http://stackoverflow.com/a/33359328/3514144 get position and make child gone so your problem of changeability on scrolling will also gone – Ajay Pandya Feb 29 '16 at 08:20
1

try this by setting clicklistener

   expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            Toast.makeText(MainActivity.this,headers.get(groupPosition)+"--"+headeritems.get(headers.get(groupPosition)).get(childPosition),Toast.LENGTH_SHORT).show();
            return false;
        }
    });

for more details refer here https://coderzpassion.com/android-expandable-listview-tutorial/

Jagjit Singh
  • 1,909
  • 1
  • 14
  • 19