可扩展列表视图将错误的子项与组位置相对



我第一次使用可扩展列表视图,所以当我获取并设置适配器以列出组时,获取整个子项目不具体。 每个组项目都有所有子项目。这里更多的是我的适配器代码。一个组项应具有自己的特定子项,但组包含所有子项。

适配器

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);
 }

试试这个,

从主活动,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);
                     }
                 }

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

相关内容

  • 没有找到相关文章

最新更新