【发布时间】:2017-01-17 13:42:08
【问题描述】:
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