Заполненный расширяемый список с вложенными JSONobjects

У меня есть работа над проектом, в котором мы должны проанализировать вложенный массив JSON и отобразить его в виде расширяемого списка, а мой файл JSON является локальным. родители населены, а дети нет. Помогите мне, пожалуйста. Мой код приведен ниже:

моя основная деятельность:

public class test extends Activity {
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
JSONObject obj, subObj;
JSONArray subcatarray, jarray;
private myExpandableAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);


    expListView = (ExpandableListView) findViewById(R.id.lvExp);

    try {


        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();


        JSONObject jo = new JSONObject(loadJSONFromAsset());

        JSONArray JA = jo.getJSONArray("list");

        for (int i = 0; i < JA.length(); i++) {
            JSONObject jop = JA.getJSONObject(i);
            listDataHeader.add(jop.getString("title"));
            JSONArray jac = jop.getJSONArray("laws");
            List<String> pSubItemArrayList = new ArrayList<String>();
            for (int j = 0; j < jac.length(); j++) {
                JSONObject jc = jac.getJSONObject(j);
                pSubItemArrayList.add(jc.getString("name"));




            }

            listDataChild.put(listDataHeader.get(i), pSubItemArrayList);

        }


    } catch (JSONException e) {
        e.printStackTrace();
    }


    adapter = new myExpandableAdapter(test.this, listDataChild, listDataHeader, expListView);

    expListView.setAdapter(adapter);
}


public String loadJSONFromAsset() {
    String json = null;
    try {
        InputStream is = getBaseContext().getAssets().open("file.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

мой адаптер:

public class myExpandableAdapter extends BaseExpandableListAdapter {
private Context context;

private List<String> listDataHeader;
private LayoutInflater inflater;
int lastExpandedGroupPosition = -1;
private HashMap<String, List<String>> listDataChild;
ExpandableListView expandableList;
public myExpandableAdapter(Context context, HashMap<String, List<String>> listDataChild, List<String> listDataHeader,ExpandableListView expandableList) {

    this.context =context;
    this.listDataChild = listDataChild;
    this.expandableList = expandableList;
    this.listDataHeader = listDataHeader;
}


@Override

public void onGroupExpanded(int groupPosition) {

    if (groupPosition != lastExpandedGroupPosition) {
        expandableList.collapseGroup(lastExpandedGroupPosition);
    }
    super.onGroupExpanded(groupPosition);
    lastExpandedGroupPosition = groupPosition;
}
@Override
public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
}

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

@Override
public int getChildrenCount(int groupPosition) {

    return listDataChild.size();
}

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

@Override
public Object getChild(int groupPosition, int childPosition)
{        return listDataChild.get(groupPosition).get(childPosition);
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    View row = convertView;

    CustomHolder holder = null;


    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.parent_items, parent, false);

        holder = new CustomHolder();

        holder.txtParentList = (TextView) row.findViewById(R.id.txtParentList);

        row.setTag(holder);
    } else {
        holder = (CustomHolder) row.getTag();
    }

    String s = listDataHeader.get(groupPosition);
    holder.txtParentList.setText(s);

    return row;
}

static class CustomHolder {
    TextView txtParentList;

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

    View row = convertView;

    CustomCHolder holderc ;

    if (row == null) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.child_items, parent, false);

        holderc = new CustomCHolder();
        holderc.txtChildList = (TextView) row.findViewById(R.id.txtChildList);

        row.setTag(holderc);
    } else {
        holderc = (CustomCHolder) row.getTag();
    }

    return row;
}

static class CustomCHolder {
    TextView txtChildList;

}

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

и мой JSON:

{  "list": [
{
  "id": 1,
  "title": "title1",
  "laws": [
    {
      "id": 1,
      "name": "name1"
    },
    {
      "id": 2,
      "name": "name2"
    }
  ]
},
{
  "id": 2,
  "title": "title2",
  "laws": [
    {
      "id": 3,
      "name": "name3"
    },
    {
      "id": 4,
      "name": "name4"
    },
    {
      "id": 5,
      "name": "name5"
    },    
     ]
}]}

1 ответ

Вы не установили данные для вашего дочернего представления в getChildView метод,

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

    String s = (String) getChild(groupPosition, childPosition);
    holder.txtChildList.setText(s);

    return row;
}
Другие вопросы по тегам