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

时间:2015-03-16 22:13:57

标签: android arrays expandablelistview

数组"反馈"包含keyword_id和反馈

例如

{keyword_id= 1, feedback="test1"},
{keyword_id=1, feedback="test2"},
{keyword_id=2, feedback="test1"}

所以,现在我想将每个keyword_id添加为listDataheader,并将其反馈添加为listDatachild。 Keyword_id = 1应该显示其2个反馈,keyword_id = 2应该显示其唯一的反馈。

我如何实现这一目标?

我当前的代码只能显示keyword_id = 1且仅反馈=" 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;
    }

}

1 个答案:

答案 0 :(得分:0)

有时让3rd party library为你做繁重的工作更容易。我建议使用NFRolodexArrayAdapter。它适用于像你这样的情况,可以从对象本身确定对象的标题。在repo和演示应用程序上有很多使用它的示例代码。

您的代码如下所示:

活动

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