如何从string.xml中的字符串数组将子项添加到ExpandableListView?

时间:2018-06-28 05:38:08

标签: android expandablelistview

我想从string.xml中的字符串数组为ExpandableListView中的不同组添加不同的子级,但是我不知道该怎么办。
这是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>

以下是我对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)
}

也许Child.java和Group.java可以提供帮助
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;
    }
}  

我尝试了多种方法将孩子添加到小组中,但是他们根本没有工作,希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

尝试一下:

    //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);
    }

希望有帮助!

编辑

    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;
        }
    });