Я пытаюсь реализовать динамические значения в расширяемом списке, но сталкиваюсь с проблемой при добавлении подэлементов в каждый основной элемент. Проблема в том, что все подпункты добавляются к каждому основному элементу, а не добавляются к соответствующему основному элементу. Вот инициализация ArrayList, который я использую -
DayBookAdapter adapter;
ArrayList<ArrayList<DayBookDetailsData>> ch_list = new ArrayList<ArrayList<DayBookDetailsData>>();
ArrayList<DayBookData> list = new ArrayList<>();
Вот код синтаксического анализа JSON и добавления основного элемента и подпункта -
try
{
JSONObject jsonrootobjct=new JSONObject(response);
JSONArray jsonArray = null;
jsonArray=jsonrootobjct.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = null;
jsonObject1= jsonArray.getJSONObject(i);
try {
category_id = jsonObject1.getString(Json_categoryid);
category_name = jsonObject1.getString(Json_categoryname);
JSONArray jsonArrayitem=null;
jsonArrayitem = jsonObject1.getJSONArray("itemdata");
for (int j = 0; j < jsonArrayitem.length(); j++) {
jsonObject1 = jsonArrayitem.getJSONObject(j);
try {
item_name = jsonObject1.getString(Json_itemname);
item_id = jsonObject1.getString(Json_itemid);
DayBookDetailsData ch = new DayBookDetailsData(item_name, item_id);
ch_list.add(ch);
}
catch (JSONException e) {
e.printStackTrace();
}
}
DayBookData gru = new DayBookData(category_id,category_name, ch_list);
list.add(gru);
adapter=new DayBookAdapter(getApplicationContext(), list);
exp_listview.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
catch (JSONException e)
{
e.printStackTrace();
}
Это мой код DayBookData-
public class DayBookData {
private String category_name;
private String category_id;
private ArrayList<DayBookDetailsData> ch_list;
public String getCategory_id() {
return category_id;
}
public String getCategory_name() {
return category_name;
}
public ArrayList<DayBookDetailsData> getCh_list() {
return ch_list;
}
public DayBookData(String category_id, String category_name, ArrayList<DayBookDetailsData> ch_list) {
this.category_id=category_id;
this.category_name=category_name;
this.ch_list=ch_list;
}
public ArrayList<DayBookDetailsData> getItems() {
return ch_list;
}
}
Это мой код DayBookDetailsData -
public class DayBookDetailsData {
String item_name;
String item_id;
public String getItem_name() {
return item_name;
}
public String getItem_id() {
return item_id;
}
public DayBookDetailsData( String item_name, String item_id) {
this.item_name=item_name;
this.item_id=item_id;
}
}
Код моего DaybookAdapter -
public class DayBookAdapter extends BaseExpandableListAdapter {
private Context c;
private ArrayList<DayBookData> list;
private LayoutInflater inflater;
public DayBookAdapter(Context c, ArrayList<DayBookData> list)
{
this.c=c;
this.list=list;
inflater=(LayoutInflater ) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
//return team.size();
return list.size();
}
@Override
public int getChildrenCount( int listPosition) {
// TODO Auto-generated method stub
//return team.get(groupPosw).players.size();
ArrayList<DayBookDetailsData> chList = list.get(listPosition).getItems();
return chList.size();
}
@Override
public Object getGroup(int listPosition) {
// TODO Auto-generated method stub
//return team.get(groupPos);
return list.get(listPosition);
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
// TODO Auto-generated method stub
//return team.get(groupPos).players.get(childPos);
ArrayList<DayBookDetailsData> chList = list.get(listPosition).getItems();
return chList.get(expandedListPosition);
}
@Override
public long getGroupId(int listPosition) {
// TODO Auto-generated method stub
//return 0;
return listPosition;
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
// TODO Auto-generated method stub
//return 0;
return expandedListPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent) {
DayBookData group = (DayBookData) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.day_book_data_item,null);
}
TextView category_id=(TextView) convertView.findViewById(R.id.category_id);
TextView category_name=(TextView) convertView.findViewById(R.id.category_name);
category_id.setText(group.getCategory_id());
category_name.setText(group.getCategory_name());
return convertView;
}
@Override
public View getChildView(int listPosition, int expandedListPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if(expandedListPosition ==0)
{
convertView = inflater.inflate(R.layout.child_header, null);
//TextView txtHeader = (TextView)convertView.findViewById(R.id.txtHeader);
//txtHeader.setText("Hello");
}
if(expandedListPosition>0 && expandedListPosition<getChildrenCount(listPosition)-1) {
DayBookDetailsData child = (DayBookDetailsData) getChild(listPosition, expandedListPosition);
LayoutInflater layoutInflater = (LayoutInflater) this.c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.day_book_details_data_item, null);
TextView item_name = (TextView) convertView.findViewById(R.id.item_name);
TextView item_id = (TextView) convertView.findViewById(R.id.item_id);
item_name.setText(child.getItem_name());
item_id.setText(child.getItem_id());
}
return convertView;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return true;
}
}
И мои данные JSON выглядят так -
"data":[
{
"c_name":"Stage And Platform",
"cat_id":"29",
"itemdata":[
{
"id":"2",
"item":" Wooden Stage"
},
{
"id":"3",
"item":" Iron Stage"
},
{
"id":"4",
"item":"Round Stage"
},
{
"id":"5",
"item":" Rotating Stage"
},
{
"id":"6",
"item":" Hydraulic Stage"
},
{
"id":"7",
"item":"Hydro-Rotating Stage"
},
{
"id":"8",
"item":"Wooden Dance Floor"
},
{
"id":"9",
"item":"Acrylic Dance Floor"
},
{
"id":"10",
"item":"Led Dance Floor"
},
{
"id":"466",
"item":"Podium"
}
]
},
{
"c_name":" Bar Set Up",
"cat_id":"30",
"itemdata":[
{
"id":"11",
"item":"Bar Chair"
},
{
"id":"12",
"item":"Bar Table"
},
{
"id":"13",
"item":"Bar Counter Wooden "
},
{
"id":"14",
"item":" Acrylic Bar Counter "
},
{
"id":"15",
"item":"Led Bar Counter"
},
{
"id":"16",
"item":"Bartender"
},
{
"id":"17",
"item":"Bar Utensils"
},
{
"id":"18",
"item":"Glass Bar Table"
}
]
}
Пожалуйста, помогите. Заранее спасибо.