2017-03-08 23 views
1

ネストされたJSON配列を解析して展開可能なリストビューで表示する必要があり、JSONファイルがローカルであるプロジェクトで作業しています。親は住んでいるが子供はいない。私を助けてください。あなたのにデータを設定していないネストされた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

私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を私のコードを以下に示します。子供ビューの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); および行: 文字列s =(String)getChild(groupPosition、childPosition); とエラーメッセージ "インタフェースメソッド 'java.lang.Object java.util.List.get(int)'をヌルオブジェクト参照で呼び出そうとしています" – fardevin

関連する問題