解析多層Beanクラスはどのように書きますか?どうやって展示しますか?

7101 ワード

ここでは、インタフェースの例を示します.http://172.17.8.100/small/commodity/v1/commodityList
解析多层次Bean类怎么写?怎么展示?_第1张图片内側から外側へ1階ずつ歩いて、内側から外側へ内側から外側へ
第1層
public class JsonBean {
private int commodityId;
private String commodityName;
private String masterPic;
private int price;
private int saleNum;

}
だいにそう
//この層には第1層public class TwoBean{が含まれています
private ArrayList commodityList;
private int id;
private String name;

}
第3層
//この層には2層目のpublic class Resultが含まれています{
private ArrayList mlss;
private ArrayList pzsh;
private ArrayList rxxp;

}
第4層
//この層には3層目のpublic class fourBeanが含まれています{
private String message;
private Result result;
private String status;

}
次はMainActivityで解析を開始し、set getメソッドを追加することを忘れないでください.
HttpUtil.getActask(url,new HttpUtil.BackA()/解析
                @Override
                public void getData(String s) {
                    Gson gson = new Gson();
                    fourBean jsonBean = gson.fromJson(s, fourBean.class);
                    Result result = jsonBean.getResult();
                    ArrayList commodityList1 = result.getMlss().get(0).getCommodityList();
                    ArrayList commodityList2 = result.getPzsh().get(0).getCommodityList();
                    ArrayList commodityList3 = result.getRxxp().get(0).getCommodityList();
                    list = new ArrayList<>();
                    list.addAll(commodityList1);
                    list.addAll(commodityList2);
                    list.addAll(commodityList3);
                    adapter = new XAdapter(MainActivity.this, list);
                    listview.setAdapter(adapter);
                    listview.stopRefresh();
                }
            });
            ****//      XListview   ****
            
             HttpUtil.getActask(url, new HttpUtil.BackA() {
                //  
                @Override
                public void getData(String s) {
                    Gson gson = new Gson();
                    fourBean jsonBean = gson.fromJson(s, fourBean.class);
                    Result result = jsonBean.getResult();
                    ArrayList commodityList1 = result.getMlss().get(0).getCommodityList();
                    ArrayList commodityList2 = result.getPzsh().get(0).getCommodityList();
                    ArrayList commodityList3 = result.getRxxp().get(0).getCommodityList();
                    ArrayList list2 = new ArrayList<>();
                    list2.addAll(commodityList1);
                    list2.addAll(commodityList2);
                    list2.addAll(commodityList3);
                    list.addAll(list2);
                    adapter.notifyDataSetChanged();
                    listview.stopLoadMore();
                }
            });
             HttpUtil.getActask(url, new HttpUtil.BackA() {
        **//         **
        @Override
        public void getData(String s) {
            Gson gson = new Gson();
            fourBean jsonBean = gson.fromJson(s, fourBean.class);
            Result result = jsonBean.getResult();
            ArrayList commodityList1 = result.getMlss().get(0).getCommodityList();
            ArrayList commodityList2 = result.getPzsh().get(0).getCommodityList();
            ArrayList commodityList3 = result.getRxxp().get(0).getCommodityList();
            list = new ArrayList<>();
            list.addAll(commodityList1);
            list.addAll(commodityList2);
            list.addAll(commodityList3);
            adapter = new XAdapter(MainActivity.this, list);
            listview.setAdapter(adapter);
        }
    });

アダプタのコードを次に示します
    @Override
public View getView(int position, View convertView, ViewGroup parent) {

    int type = getItemViewType(position);
    switch (type){
        case 0:
            ViewHolder1 holder1 = null;
            if (convertView==null){
                convertView = View.inflate(context,R.layout.item01,null);
                holder1 = new ViewHolder1();
                holder1.imageView1 = convertView.findViewById(R.id.imageview1);
                holder1.textView1 = convertView.findViewById(R.id.textview1);
                convertView.setTag(holder1);
            }else{
                holder1 = (ViewHolder1) convertView.getTag();
            }
            JsonBean jsonBean = list.get(position);
            holder1.textView1.setText(jsonBean.getCommodityName());
            Glide.with(convertView).load(jsonBean.getMasterPic()).into(holder1.imageView1);
            break;
        case 1:
            ViewHolder2 holder2 = null;
            if (convertView==null){
                convertView = View.inflate(context,R.layout.item02,null);
                holder2 = new ViewHolder2();
                holder2.imageView2 = convertView.findViewById(R.id.imageview1);
                holder2.textView2 = convertView.findViewById(R.id.textview1);
                convertView.setTag(holder2);
            }else{
                holder2 = (ViewHolder2) convertView.getTag();
            }
            JsonBean jsonBean2 = list.get(position);
            holder2.textView2.setText(jsonBean2.getCommodityName());
            Glide.with(convertView).load(jsonBean2.getMasterPic()).into(holder2.imageView2);
            break;
        case 2:
            ViewHolder3 holder3 = null;
            if (convertView==null){
                convertView = View.inflate(context,R.layout.item03,null);
                holder3 = new ViewHolder3();
                holder3.imageView3 = convertView.findViewById(R.id.imageview1);
                holder3.textView3 = convertView.findViewById(R.id.textview1);
                convertView.setTag(holder3);
            }else{
                holder3 = (ViewHolder3) convertView.getTag();
            }
            JsonBean jsonBean3 = list.get(position);
            holder3.textView3.setText(jsonBean3.getCommodityName());
            Glide.with(convertView).load(jsonBean3.getMasterPic()).into(holder3.imageView3);
            break;
    }


    return convertView;
}


class ViewHolder1{
    ImageView imageView1;
    TextView textView1;
}
class ViewHolder2{
    ImageView imageView2;
    TextView textView2;
}
class ViewHolder3{
    ImageView imageView3;
    TextView textView3;
}
@Override
public int getViewTypeCount() {
    return 3;
}

@Override
public int getItemViewType(int position) {
    return position%3;
}

}