ExpandableListView错误的孩子反对组位置

时间:2018-01-10 13:06:39

标签: android json listview expandable

我第一次使用expandablelistview,所以当我得到并设置适配器列出组时,整个子项不是特定的。每个组项都包含所有子项。更多的是我的适配器代码。一个组项应具有自己的特定子项,但组包含所有子项。

适配器

public class RestaurantMenuAdapter extends BaseExpandableListAdapter {
Context context;
ArrayList<RestaurantParentModel>ListTerbaru;
ArrayList<ArrayList<RestaurantChildModel>> ListChildTerbaru;
int count;

public RestaurantMenuAdapter (Context context, ArrayList<RestaurantParentModel> ListTerbaru, ArrayList<ArrayList<RestaurantChildModel>> ListChildTerbaru){
    this.context=context;
    this.ListTerbaru=ListTerbaru;
    this.ListChildTerbaru=ListChildTerbaru;

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


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

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


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

    RestaurantChildModel childTerbaru = getChild(groupPosition, childPosition);
    RestaurantMenuAdapter.ViewHolder holder= null;

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.row_item_restaurant_child, null);

        holder=new RestaurantMenuAdapter.ViewHolder();
        holder.title_name_child=(TextView)convertView.findViewById(R.id.title_name_child);
        holder.sub_title_name_child = convertView.findViewById(R.id.sub_title_name_child);
        holder.price_tv = convertView.findViewById(R.id.price_tv);

        convertView.setTag(holder);

    }
    else{
        holder=(RestaurantMenuAdapter.ViewHolder)convertView.getTag();
    }

    holder.title_name_child.setText(childTerbaru.getChild_title());
    holder.sub_title_name_child.setText(childTerbaru.getChild_sub_title());
    holder.price_tv.setText(childTerbaru.getPrice());


    return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
    return ListChildTerbaru.get(groupPosition).size();
}@Override
public RestaurantParentModel getGroup(int groupPosition) {
    return ListTerbaru.get(groupPosition);
}

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

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    RestaurantParentModel terbaruModel =  getGroup(groupPosition);
    RestaurantMenuAdapter.ViewHolder holder= null;
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.row_item_restaurant_parent, null);

        holder=new RestaurantMenuAdapter.ViewHolder();
        holder.title_name=(TextView)convertView.findViewById(R.id.title_name);
        holder.sub_title_name=(TextView)convertView.findViewById(R.id.sub_title_name);
        convertView.setTag(holder);

    }

    else{
        holder=(RestaurantMenuAdapter.ViewHolder)convertView.getTag();
    }

    holder.title_name.setText(terbaruModel.getTitle());
    holder.sub_title_name.setText(terbaruModel.getSub_title());


    return convertView;
}

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

@Override
public boolean isChildSelectable(int arg0, int arg1) {
    return true;
}


static class ViewHolder{
    TextView title_name,sub_title_name,title_name_child,sub_title_name_child,price_tv;
}
}

 // And how i am calling this adapter from activity is here bellow.




 listDataHeader = new ArrayList<>();
 listChildData = new ArrayList<>();
 ListChild = new ArrayList<>();
 String id = res_menuItemPref.getString(PreferenceClass.RESTAURANT_ID,"");
 RequestQueue queue = Volley.newRequestQueue(getContext());
 JSONObject jsonObject = new JSONObject();

 try {
     jsonObject.put("id",id);
 } catch (JSONException e) {
     e.printStackTrace();
 }

 JsonObjectRequest resMenuItemRequest = new JsonObjectRequest(Request.Method.POST, Config.SHOW_RESTAURANT_MENU, jsonObject, new Response.Listener<JSONObject>() {
     @Override
     public void onResponse(JSONObject response) {


         String strJson =  response.toString();
         JSONObject jsonResponse = null;
         try {
             jsonResponse = new JSONObject(strJson);

             Log.d("JSONPost", jsonResponse.toString());

             int code_id = Integer.parseInt(jsonResponse.optString("code"));

             if (code_id == 200) {

                 JSONObject json = new JSONObject(jsonResponse.toString());
                 JSONArray jsonArray = json.getJSONArray("msg");

                 for (int i = 0; i < jsonArray.length(); i++) {

                        JSONObject allJsonObject1 = jsonArray.getJSONObject(i);
                        JSONObject currency = allJsonObject1.getJSONObject("Currency");
                        String currency_symbol = currency.optString("symbol");
                        JSONObject coverImage = allJsonObject1.getJSONObject("Restaurant");
                        String coverImgURL = coverImage.optString("cover_image");
                     Picasso.with(getContext()).load(Config.imgBaseURL+coverImgURL).
                             fit().centerCrop()
                             .placeholder(R.mipmap.ic_launcher)
                             .error(R.drawable.unknown_deal).into(cover_image);

                     JSONArray resMenuArray = allJsonObject1.getJSONArray("RestaurantMenu");

                     for (int j =0; j<resMenuArray.length();j++){

                         JSONObject resMenuAllObj = resMenuArray.getJSONObject(j);

                        RestaurantParentModel restaurantParentModel = new RestaurantParentModel();

                        restaurantParentModel.setTitle(resMenuAllObj.optString("name"));
                        restaurantParentModel.setSub_title(resMenuAllObj.optString("description"));

                        listDataHeader.add(restaurantParentModel);


                         JSONArray menuItemArray = resMenuAllObj.getJSONArray("RestaurantMenuItem");
                         for(int k=0;k<menuItemArray.length();k++){
                             JSONObject menuItemArrayObj = menuItemArray.getJSONObject(k);

                             RestaurantChildModel restaurantChildModel = new RestaurantChildModel();

                             restaurantChildModel.setChild_title(menuItemArrayObj.optString("name"));
                             restaurantChildModel.setChild_sub_title(menuItemArrayObj.getString("description"));
                             restaurantChildModel.setPrice(currency_symbol+menuItemArrayObj.optString("price"));

                             listChildData.add(restaurantChildModel);
                             ListChild.add(listChildData);

                         }

                     }

                     restaurantMenuAdapter = new RestaurantMenuAdapter(getContext(), listDataHeader, ListChild);
                     // setting list adapter
                     expandableListView.setAdapter(restaurantMenuAdapter);
                   /*  for(int m=0; m < restaurantMenuAdapter.getGroupCount(); m++)
                         expandableListView.expandGroup(m);*/


                 }

             }
         }
         catch (Exception e){
             e.getMessage();
         }
     }
 }, new Response.ErrorListener() {
     @Override
     public void onErrorResponse(VolleyError error) {
         Toast.makeText(getContext(),error.toString(),Toast.LENGTH_LONG).show();
     }
 });

 queue.add(resMenuItemRequest);

 }

2 个答案:

答案 0 :(得分:0)

您是否尝试记录适配器的条目? 乍一看,您的适配器似乎没有任何错误,但您确定要向用户显示的数据是否应该在适配器中到达?

答案 1 :(得分:0)

试试这个,

来自主要活动,oncreate方法,

仅启动父数组列表

listDataHeader = new ArrayList<>();

然后,在完成此行之后

for (int j =0; j<resMenuArray.length();j++){

                     JSONObject resMenuAllObj = resMenuArray.getJSONObject(j);

                    RestaurantParentModel restaurantParentModel = new RestaurantParentModel();

                    restaurantParentModel.setTitle(resMenuAllObj.optString("name"));
                    restaurantParentModel.setSub_title(resMenuAllObj.optString("description"));

                    listDataHeader.add(restaurantParentModel);

现在启动子数组列表

listChildData = new ArrayList<>();

然后,

JSONArray menuItemArray = resMenuAllObj.getJSONArray("RestaurantMenuItem");
                     for(int k=0;k<menuItemArray.length();k++){
                         JSONObject menuItemArrayObj = menuItemArray.getJSONObject(k);

                         RestaurantChildModel restaurantChildModel = new RestaurantChildModel();

                         restaurantChildModel.setChild_title(menuItemArrayObj.optString("name"));
                         restaurantChildModel.setChild_sub_title(menuItemArrayObj.getString("description"));
                         restaurantChildModel.setPrice(currency_symbol+menuItemArrayObj.optString("price"));

                         listChildData.add(restaurantChildModel);
                         ListChild.add(listChildData);

                     }

                 }