如何在运行时在Android中填充ExpandableListView?

如何在运行时在Android中填充ExpandableListView?,android,arrays,expandablelistview,Android,Arrays,Expandablelistview,数组反馈包含关键字_id和反馈 例如 因此,现在我想将每个关键字_id添加为listDataheader,并将其反馈添加为listDatachild。关键字_id=1应显示其2个反馈,关键字_id=2应显示其唯一反馈 我怎样才能做到这一点 我当前的代码只能显示关键字_id=1和feedback=test1 try { JSONObject jsonResponse; jsonResponse = new JSONObject(res

数组反馈包含关键字_id和反馈

例如

因此,现在我想将每个关键字_id添加为listDataheader,并将其反馈添加为listDatachild。关键字_id=1应显示其2个反馈,关键字_id=2应显示其唯一反馈

我怎样才能做到这一点

我当前的代码只能显示关键字_id=1和feedback=test1

        try {
            JSONObject jsonResponse;
            jsonResponse = new JSONObject(result);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("feedbacks");
            if(jsonMainNode != null) {
                int lengthJsonArr = jsonMainNode.length();
                for (int i = 0; i < lengthJsonArr; i++) {
                    listDataHeader = new ArrayList<String>();
                    listDataChild = new HashMap<String, List<String>>();
                    JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                    listDataHeader.add(jsonChildNode.getString("keyword_id"));
                    List<String> key1 = new ArrayList<String>();
                    key1.add(jsonChildNode.getString("feedback"));
                    listDataChild.put(listDataHeader.get(0), key1);
                }
                listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
                expListView.setAdapter(listAdapter);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
完整适配器类

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader;
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

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

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

有时候,让一个男人帮你做重担更容易。我建议使用NFROLODEXARRAYAAdapter。它非常适用于像您这样的情况,即可以从对象本身确定对象的标题。在repo和一个演示应用程序中有大量的示例代码使用它

您的代码如下所示:

活动

适配器


您的ExpandableListAdapter实现在哪里?@AndrewOrobator更新了AdapterOhkay我会试试这个。谢谢
public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader;
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

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

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}
    //Convert your JSONArray to an ArrayList
    ArrayList<JSONObject> listdata = new ArrayList<>();     
    int lengthJsonArr = jsonMainNode.length();
    for (int i = 0; i < lengthJsonArr; ++i){ 
        listdata.add(jsonMainNode.getJSONObject(i));
    }

    //Just pass in your ArrayList of JSONObjects. Adapter will handle sorting into the appropriate headers
    MyExpandableListAdapter adapter = new MyExpandableListAdapter(this, listdata);
    expListView.setAdapter(adapter);
private class MyExpandableListAdapter extends NFRolodexArrayAdapter<String, JSONObject> {

    public MyExpandableListAdapter(Context activity, Collection<JSONObject> items) {
        super(activity, items);
    }

    @Override
    public Date createGroupFor(JSONObject childItem) {
        //This is how the adapter determines what the headers are and what child items belong to it
        return childItem.getString("keyword_id");
    }

    @Override
    public View getChildView(LayoutInflater inflater, int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        //Inflate your view

        //Get the JSONObject data for this view
        JSONObject data = getChild(groupPosition, childPosition);

        //Fill view with data
    }

    @Override
    public View getGroupView(LayoutInflater inflater, int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        //Inflate your header view

        //Gets the Header String for this view
        String headerTitle = getGroup(groupPosition);

        //Fill view with headerTitle
    }
}