【问题标题】:Horizontal and vertical recyclerview parsing json data in AndroidAndroid中横向和纵向recyclerview解析json数据
【发布时间】:2017-01-17 13:42:08
【问题描述】:

See sanpshot here

jObject = new JSONObject(result);
                    JSONObject resultObj = jObject.getJSONObject("result");
                    


  for(int i = 0; i < firstproducts.size(); i++) {

                           try {
                               title = firstproducts.get(i).getString("category");
                               categoryIcon = firstproducts.get(i).getString("icon_img");
                               Log.e("For I","Value : "+title);
      for(int j = 0; j < secondproducts.size(); j++) {
                                   if(title.equalsIgnoreCase(secondproducts.get(j).getString("category"))){
                                     
  categoryIcon = secondproducts.get(j).getJSONObject("info").getString("img");
                                       

          JSONArray products=null;
     products = firstproducts.get(j).gptJSONArray("Products");                          if(products!=null) {
         
             for (int k = 0; k < products.length(); k++) {
                                               
                                             
  JSONObject jsonObj = products.getJSONObject(k);

                                             
          content = jsonObj.getString("description");
       points = jsonObj.getString("points");
       imageUrl = jsonObj.getString("image");
                                           
                                       
         secondarraylist.add(new ChildModel(content,points,rating,imageUrl));
              }                         
}
   mainarraylist.add(new MainModel(title,categoryIcon,secondarraylist));
                                     finally i set this above main arraylist to recyclerview adapter
------------------------------- ---------
                                       
Mian Model class as below -child model inside 3 string values will be there.

public class MainModel {
    String title;
    String categoryIcon;
    private List<ChildModel> ChildItems;

    public MainModel() {
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCategoryIcon() {
        return categoryIcon;
    }

    public void setCategoryIcon(String categoryIcon) {
        this.categoryIcon = categoryIcon;
    }

    public List<ChildModel> getChildItems() {
        return ChildItems;
    }

    public void setItems(List<ChildModel> ChildItems) {
        this.ChildItems = ChildItems;
    }
}
---------------------------------------
      Main adapter       
      
      
      public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ProductViewHolder> {

    private final Context mContext;
    List<MainModel> maindata;

    public MainAdapter(Context mContext, List<MainModel> ProductList) {
        this.mContext = mContext;
        this.maindata=ProductList;
    }


    @Override
    public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.product_list_store, parent, false);
        return new ProductViewHolder(view);

    }

    public class ProductViewHolder extends RecyclerView.ViewHolder {

        public final TextView title;
        public final ImageView categoryIcon;
        public final ImageView leftArrow;
        public final ImageView rightArrow;
        public final View categoryDiver;
        private ChildAdapter horizontalAdapter;
        private RecyclerView horizontalList;
        private LinearLayoutManager llm;


        public ProductViewHolder(View view) {
            super(view);
            Context context = itemView.getContext();
            title = (TextView) view.findViewById(R.id.product_textForHeader);
            categoryIcon = (ImageView) view.findViewById(R.id.product_Iconimageview);
            leftArrow = (ImageView)view.findViewById(R.id.product_leftside_arrow);
            rightArrow = (ImageView)view.findViewById(R.id.product_rightside_arrow);
            categoryDiver = view.findViewById(R.id.product_Divider_view);
            horizontalList = (RecyclerView) itemView.findViewById(R.id.horizontal_layout);
            llm = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
            horizontalList.setLayoutManager(llm);

            horizontalAdapter = new ChildAdapter(context);
            horizontalList.setAdapter(horizontalAdapter);


        }
    }

    @Override
    public void onBindViewHolder(final ProductViewHolder holder, int position) {

        holder.title.setText(maindata.get(position).getTitle());
        holder.horizontalAdapter.setData(maindata.get(position).getStoreItems()); // List of Strings
        holder.horizontalAdapter.setRowIndex(position);
        holder.leftArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("left");
                if (holder.llm.findFirstCompletelyVisibleItemPosition() > 0){
                    holder.horizontalList.smoothScrollToPosition(holder.llm.findFirstVisibleItemPosition() - 1);
                }
            }
        });

        holder.rightArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("right");
                holder.horizontalList.smoothScrollToPosition(holder.llm.findFirstVisibleItemPosition() + 1);
            }
        });

        if(position == maindata.size()-1)
            holder.categoryDiver.setVisibility(View.GONE);

  }
    @Override
    public int getItemCount() {
       //return 0;
        return maindata.size();
    }

}
-------------------------------------------
  
  ChildAdapter

public class ChildAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;
    private List<ChildModel> childdata;
    private ImageLoader imageLoader;
    private int mRowIndex = -1;

    public ChildAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<ChildModel> data) {
        if (childdata != data) {
            childdata = data;
            notifyDataSetChanged();
        }
    }
    publindex;
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {

        public final TextView content;
        public final TextView rating;
        public final TextView points;
        public final NetworkImageView img_item;
        public final ImageView img_rating;


        public ItemViewHolder(View view) {
            super(view);
            content = (TextView) view.findViewById(R.id.category_item_title);
            rating = (TextView) view.findViewById(R.id.text_rating);
            points = (TextView) view.findViewById(R.id.category_points);
            img_item = (NetworkImageView) view.findViewById(R.id.category_item_imageView);
            img_rating= (ImageView) view.findViewById(R.id.img_rating);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View itemView = LayoutInflater.from(context).inflate(R.layout.store_category_item, parent, false);
        ItemViewHolder holder = new ItemViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewholder, int position) {

        ItemViewHolder holder=(ItemViewHolder)viewholder;
        holder.content.setText(childdata.get(position).getContent());
        holder.points.setText(childdata.get(position).getPoints());
        holder.rating.setText(childdata.get(position).getRating());

        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return  childdata.size();
    }
}

                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                     

我正在做一个水平和垂直回收视图的项目,就像一个游戏商店一样。我已经完成了所有的设计,比如 2 个适配器和 2 个模型类。

问题是我解析了 json 并将所有数据设置为 2 个数组列表,因此所有数据都进入水平适配器。如何根据下面的动态设置来自arraylist的水平值。

这是我的 API:自行车、汽车、卡车;我需要设置这个数据垂直recyclerview标题和内部产品数组,即水平recyclerview:

【问题讨论】:

    标签: android json parsing arraylist


    【解决方案1】:

    我可以告诉你方法-->在主数组中获取所有子 json-->然后在每个这些 json 对象中获取产品 jsonobject 并相应地设置它们

    【讨论】:

    • 您好,感谢您的回答。正是我的问题是我将所有子数据解析为数组列表,最后我设置为邮件数组列表,所以我得到水平的输出,所有的子数据都是垂直的recyclerview。请与我分享您的代码完全 json 解析,设置为 arraylist,最后设置为适配器。请使用我的邮件 ID (prasanthjune15@gmail.com) 发送您的代码或在此页面中发布。再次感谢您的评论。
    • 实际上我无法理解您的列表会是什么样子。即哪些孩子被设置为标题,哪些是水平的,......你能给我一个原始图表吗?
    • 正是我的问题出在我的 api 自行车、汽车和卡车中,所有产品数组值都以水平方向聚集在一起,我的意思是产品 [1]、产品 [8] 和产品 [2] 即将到来一起,因为我的东西在 arraylist 中,但我不知道如何拆分并添加到 recyclerview 垂直适配器。我在垂直适配器中组合了水平适配器,请参阅我的代码。
    猜你喜欢
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    相关资源
    最近更新 更多