简体   繁体   中英

How to add child to an ExpandableListView from string arrays in string.xml?

I want to add different childs for different group in the ExpandableListView from the string arrays in string.xml , but I don't know what should I do.
Here are the contents of string.xml :

<resources>
    <string name="app_name">app_name</string>

    <string-array name="child_group_1">
        <item>Item1</item>
        <item>Item2</item>
        <item>Item3</item>
        <item>Item4</item>
        <item>Item5</item>
    </string-array>

    <string-array name="child_group_2">
        <item>Item1</item>
        <item>Item2</item>
        <item>Item3</item>
        <item>Item4</item>
        <item>Item5</item>
    </string-array>
</resources>

The following is my custom Adapter for ExpandableListView :
MyAdapter.java

public class MyAdapter extends BaseExpandableListAdapter {
    /**Group name*/
    private ArrayList<String> groups;

    /**Child*/
    private ArrayList<ArrayList<Child>> childs;
    private Context mContext;
    public MyAdapter(Context context){
        mContext = context;
        init();
    }
    public void init(){
        //Add some groups
        groups = new ArrayList<String>();
        groups.add("Group 1");
        groups.add("Group 2");

        //Add some child
        childs = new ArrayList<ArrayList<Child>>();
        Child child = new Child();
        ArrayList<Child> valuesEAL = new ArrayList<>();
    }
    @Override
    public int getGroupCount() {
        return groups.size();
    }

     ...(Omitted)

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        /**Create view */
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_group,null);
        /** Find views */
        TextView tvGroupTitle = (TextView)v.findViewById(R.id.tv_G_txtDecr);
        ImageView ivGroupImg = (ImageView)v.findViewById(R.id.tv_img_Indicator);
        tvGroupTitle.setText(groups.get(groupPosition));

        // Setting group indicators
        if(groupPosition == 0){
           ivGroupImg.setImageResource(R.drawable.indi_1);
        } else if (groupPosition == 1) {
        ivGroupImg.setImageResource(R.drawable.indi_2);
        }

        return v;
    }

    ...(Omitted)
}

Maybe the Child.java and the Group.java can help
Child.java :

public class Child {
    // Title
    private String title;

    public Child(){
        title = "title";
    }

    public String getTitle() {
        return title;
    }


    public void setTitle(String title) {
        this.title = title;
    }


}

Group.java :

public class Group {
    private String title;
    public Group(){
        title="title";
    }

    public String getTitle(){
     return title;
    }

    public void setTitle(String title){
        this.title=title;
    }
}  

I tried many ways to add the child to the groups but they did not work at all, hope someone can help me.

Try this:

    //Add some child
    childs = new ArrayList<>();
    for(int i=1; i<=getGroupCount(); i++){
        String[] tmpGroup = mContext.getResources().getStringArray(mContext.getResources().getIdentifier("child_group_"+i,
                "array", mContext.getPackageName()));
        ArrayList<Child> valuesEAL = new ArrayList<>();
        for(int j=0; j<tmpGroup.length; j++){
            Child child = new Child();
            child.setTitle(tmpGroup[j]);
            valuesEAL.add(child);
        }
        childs.add(valuesEAL);
    }

Hope that helps!

EDIT

    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
            MyAdapter adapter = (MyAdapter)expandableListView.getExpandableListAdapter();
            String clickedChildTitle = ((Child)adapter.getChild(i, i1)).getTitle();

            return true;
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM