Search code examples
androidlistviewexpandablelistviewexpandablelistadapter

how to set the childView of the ExpandableListView in mainActivity


I just finished implementing my custom ExpandableListView Adapter, now in the main activity I have no Idea how to write the codes for my 2 custom ArrayLists(one for the groupView and another for childView). GroupView of the ExpandableListView works fine but I don't know how should I write the code and set the data of the childView .

let's consider all the data of the Group and Child views exactly like the same as this:

X.add(new Word(R.drawable.ic_launcher_background,"A" , "#11111"));

so there are two texts and one img in the rows of both child and group views of the ExpandableListView.

-Thanks

MainActivity.java

public class MainActivity extends Activity {

    private ExpandListAdapter ExpAdapter;
    private ArrayList<Word> ExpListItems;
    private ExpandableListView ExpandList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ExpandList = (ExpandableListView) findViewById(R.id.lvExp);
        ExpAdapter = new ExpandListAdapter(MainActivity.this, ExpListItems);

        ExpListItems = new ArrayList<Word>();
        ExpListItems.add(new Word(R.drawable.ic_launcher_background,"A" , "#11111"));
        ExpListItems.add(new Word(R.drawable.ic_launcher_background,"B" , "#11111"));
        ExpListItems.add(new Word(R.drawable.ic_launcher_background,"C" , "#11111"));
        ExpListItems.add(new Word(R.drawable.ic_launcher_background,"D" , "#11111"));
        ExpListItems.add(new Word(R.drawable.ic_launcher_background,"E" , "#11111"));
        ExpListItems.add(new Word(R.drawable.ic_launcher_background,"F" , "#11111"));

        ExpandList.setAdapter(ExpAdapter);


    }

ExpandListAdapter.java (unnecessary codes are removed)

    public class ExpandListAdapter extends BaseExpandableListAdapter {
    public Context context;
    public ArrayList<Word> groups ;

        public ExpandListAdapter(Context context, ArrayList<Word> groups) {
            this.context = context;
            this.groups = groups;
        }

        @Override
        public int getGroupCount() {
            return groups.size();
        }

        @Override
        public int getChildrenCount(int i) {
            ArrayList<Word> childList = groups.get(i).getItems();
            return childList.size();
        }



        @Override
        public Object getChild(int i, int i1) {
            ArrayList<Word> childList = groups.get(i).getItems();
            return childList.get(i1);
        }
    .
.
.


        @Override
        public long getChildId(int i, int i1) {
            return i1;
        }

        .
        .
        .
        .

        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            View listItemView = view;
            if(listItemView == null) {
                LayoutInflater inflater = (LayoutInflater) this.context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                assert inflater != null;
                listItemView = inflater.inflate(R.layout.child_row,null);
            }

            Word currentWord = groups.get(i);
            TextView nameText = (TextView) listItemView.findViewById(R.id.childNameView);
            nameText.setText(currentWord.getBakhsh());


            TextView numberText = (TextView) listItemView.findViewById(R.id.childNumView);
            numberText.setText(currentWord.getNumber());


            ImageView imageIcons = (ImageView) listItemView.findViewById(R.id.childImageView);
            if (currentWord.hasImage()) {
                imageIcons.setImageResource(currentWord.getImage());


            return listItemView;

        }

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

Solution

  • You may put child items into a map with an unique field from the group as KEY, so may be like this:

    onCreate():

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ExpandList = (ExpandableListView) findViewById(R.id.lvExp);
    
        ExpListItems = new ArrayList<Word>();
        ExpListItems.add(new Word(R.drawable.ic_launcher_background, "A" , "#11111"));
        ExpListItems.add(new Word(R.drawable.ic_launcher_background, "B" , "#11111"));
        ExpListItems.add(new Word(R.drawable.ic_launcher_background, "C" , "#11111"));
        ExpListItems.add(new Word(R.drawable.ic_launcher_background, "D" , "#11111"));
        ExpListItems.add(new Word(R.drawable.ic_launcher_background, "E" , "#11111"));
        ExpListItems.add(new Word(R.drawable.ic_launcher_background, "F" , "#11111"));
        Map<String, List<Word>> mapChild = new HashMap<>();
        for(Word word: ExpListItems){
            List<Word> listChild = new ArrayList<>();
            listChild.add(new Word(R.drawable.ic_launcher_background, "Aa" , "#11111"));
            listChild.add(new Word(R.drawable.ic_launcher_background, "Bb" , "#11111"));
            listChild.add(new Word(R.drawable.ic_launcher_background, "Cc" , "#11111"));
            mapChild.put(word.getBakhsh(), listChild);
        }
    
        ExpAdapter = new ExpandListAdapter(MainActivity.this, ExpListItems, mapChild);
        ExpandList.setAdapter(ExpAdapter);
    }
    

    Your adapter:

    public class ExpandListAdapter extends BaseExpandableListAdapter {
    public Context context;
    public ArrayList<Word> groups ;
    public Map<String, List<Word>> childs;
    
    public ExpandListAdapter(Context context, ArrayList<Word> groups, Map<String, List<Word>> childs) {
        this.context = context;
        this.groups = groups;
        this.childs = childs;
    }
    
    @Override
    public int getGroupCount() {
        return groups.size();
    }
    
    @Override
    public int getChildrenCount(int i) {
        return childs.get(groups.get(i).getBakhsh()).size();
    }
    
    @Override
    public Word getGroup(int i) {
        return groups.get(i);
    }
    
    @Override
    public Word getChild(int i, int i1) {
        return childs.get(groups.get(i).getBakhsh()).get(i1);
    }
    
    @Override
    public long getGroupId(int i) {
        return i;
    }
    
    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }
    
    @Override
    public boolean hasStableIds() {
        return false;
    }
    
    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        View listItemView = view;
        if (listItemView == null) {
            LayoutInflater inflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            assert inflater != null;
            listItemView = inflater.inflate(R.layout.child_row, null);
        }
    
        Word currentWord = getGroup(i);
        TextView nameText = (TextView) listItemView.findViewById(R.id.childNameView);
        nameText.setText(currentWord.getBakhsh());
    
        TextView numberText = (TextView) listItemView.findViewById(R.id.childNumView);
        numberText.setText(currentWord.getNumber());
    
        ImageView imageIcons = (ImageView) listItemView.findViewById(R.id.childImageView);
        if (currentWord.hasImage()) {
            imageIcons.setImageResource(currentWord.getImage());
        }
        return listItemView;
    }
    
    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        View listItemView = view;
        if (listItemView == null) {
            LayoutInflater inflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            assert inflater != null;
            listItemView = inflater.inflate(R.layout.child_row, null);
        }
    
        Word currentWord = getChild(i,i1);
        TextView nameText = (TextView) listItemView.findViewById(R.id.childNameView);
        nameText.setText(currentWord.getBakhsh());
    
        TextView numberText = (TextView) listItemView.findViewById(R.id.childNumView);
        numberText.setText(currentWord.getNumber());
    
        ImageView imageIcons = (ImageView) listItemView.findViewById(R.id.childImageView);
        if (currentWord.hasImage()) {
            imageIcons.setImageResource(currentWord.getImage());
        }
        return listItemView;
    }
    
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
    }  
    

    I answered another question about ExpandableListView here: Tree with checkBox , you can try that sample too. Hope that helps!