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

问题描述 投票:0回答:1

我想在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;
    }
}  

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

android expandablelistview
1个回答
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;
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.