2017-03-08 71 views
1

我有一個項目,我們必須解析一個嵌套的JSON數組,並將其顯示在可擴展列表視圖中,而我的JSON文件是本地的。父母正在填充,但孩子不是。請幫幫我。我下面的代碼給出:嵌套JSONobjects填充可擴展列表視圖

我Main_Activity:

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" 
    },  
    ] 
}]} 

回答

0

您沒有將數據設置爲您子視圖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; 
} 
+0

謝謝。我添加這些行,但它在方法getchild在行上有錯誤: return listDataChild.get(groupPosition).get(childPosition); 和line: String s =(String)getChild(groupPosition,childPosition); 並且錯誤顯示「試圖調用接口方法java.lang.Object java.util.List.get(int)'對一個空對象引用」 – fardevin