1
중첩 된 JSON 배열을 구문 분석하여 확장 가능한 목록보기로 표시하고 JSON 파일이 로컬 인 프로젝트에서 작업하고 있습니다. 부모는 살고 있지만 아이들은 살고 있지 않습니다. 도와주세요. 내 코드는 아래와 같습니다 :중첩 된 JSON 객체가있는 확장 된 목록보기
내 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"
},
]
}]}
감사합니다. 이 줄을 추가하지만 줄에 getchild 메서드에 오류가 있습니다. return listDataChild.get (groupPosition) .get (childPosition); 및 줄 : String s = (String) getChild (groupPosition, childPosition); 및 오류 메시지 : "인터페이스 메소드 'java.lang.Object java.util.List.get (int)'을 null 객체 참조에 호출하려고 시도 함" – fardevin