多条目展示



JSONObject obj = new JSONObject(in);
JSONArray arr = obj.getJSONArray("data");
for (int x = 0; x < arr.length(); x++) {
    JSONObject json = arr.getJSONObject(x);
    NewsData newss = new NewsData();
    String title = json.getString("title");
    String source = json.getString("source");
    String comment_count = json.getString("comment_count");
    String has_image = json.getString("has_image");
    newss.setTitle(title);
    newss.setSource(source);
    newss.setUrl(json.getString("article_url"));
    newss.setComment_count(comment_count);
    newss.setHas_image(has_image);
    JSONArray image = json.getJSONArray("image_list");
    JSONObject o = json.getJSONObject("middle_image");
    if (o.isNull("url")) {
        newss.setMiddle_image("");
    } else {
        String middle_url = o.getString("url");
        newss.setMiddle_image(middle_url);
    }
    image_list = new ArrayList<String>();
    for (int j = 0; j < image.length(); j++) {
        JSONObject m = image.getJSONObject(j);
        String url = m.getString("url");
        image_list.add(url);
    }
    newss.setImg_url(image_list);
    data.add(newss);
}

public class FragmenListViewAdapter extends BaseAdapter {

    private Context context;
    private List<NewsData> data;
    private DisplayImageOptions options;

    public FragmenListViewAdapter(Context context, List<NewsData> data) {
        this.context = context;
        this.data = data;
        ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
        options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_tip_mobile)
                .showImageOnFail(R.drawable.ic_tip_pwd).cacheInMemory(true)
                .cacheOnDisk(true).build();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    //获得具体类型
    @Override
    public int getItemViewType(int position) {
        NewsData newss = data.get(position);
        String hasimage = newss.getHas_image();
        ArrayList<String> list = newss.getImg_url();
        if (hasimage.equals("false")) {
            return 0;
        }
        if (hasimage.equals("true") && list.size() == 0) {
            return 1;
        }
        if (hasimage.equals("true") && list.size() > 0) {
            return 2;
        }
        return super.getItemViewType(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder1 holder1 = null;
        ViewHolder2 holder2 = null;
        ViewHolder3 holder3 = null;
        NewsData newss = data.get(position);
        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case 0:
                    convertView = View.inflate(context, R.layout.item1, null);
                    holder1 = new ViewHolder1();
                    holder1.title1 = (TextView) convertView.findViewById(R.id.tv_title1);
                    holder1.source1 = (TextView) convertView.findViewById(R.id.tv_source1);
                    holder1.comment_count1 = (TextView) convertView.findViewById(R.id.tv_comment_count1);
                    convertView.setTag(holder1);
                    break;
                case 1:
                    convertView = View.inflate(context, R.layout.item2, null);
                    holder2 = new ViewHolder2();
                    holder2.title2 = (TextView) convertView.findViewById(R.id.tv_title_show);
                    holder2.source2 = (TextView) convertView.findViewById(R.id.tv_source_show);
                    holder2.comment_count2 = (TextView) convertView.findViewById(R.id.tv_comment_show);
                    holder2.img_img2 = (ImageView) convertView.findViewById(R.id.frg_image_view);
                    convertView.setTag(holder2);
                    break;
                case 2:
                    convertView = View.inflate(context, R.layout.item3, null);
                    holder3 = new ViewHolder3();
                    holder3.title3 = (TextView) convertView.findViewById(R.id.tv_title3);
                    holder3.source3 = (TextView) convertView.findViewById(R.id.tv_source3);
                    holder3.comment_count3 = (TextView) convertView.findViewById(R.id.tv_comment_count3);
                    holder3.time3 = (TextView) convertView.findViewById(R.id.tv_time3);
                    holder3.img1 = (ImageView) convertView.findViewById(R.id.img1);
                    holder3.img2 = (ImageView) convertView.findViewById(R.id.img2);
                    holder3.img3 = (ImageView) convertView.findViewById(R.id.img3);
                    convertView.setTag(holder3);
                    break;
            }
        } else {
            switch (type) {
                case 0:
                    holder1 = (ViewHolder1) convertView.getTag();
                    break;
                case 1:
                    holder2 = (ViewHolder2) convertView.getTag();
                    break;
                case 2:
                    holder3 = (ViewHolder3) convertView.getTag();
                    break;
            }
        }
        switch (type) {
            case 0:
                holder1.title1.setText(newss.getTitle());
                holder1.source1.setText("来源:" + newss.getSource());
                holder1.comment_count1.setText("评论:" + newss.getComment_count());
                break;
            case 1:
                holder2.title2.setText(newss.getTitle());
                holder2.source2.setText("来源:" + newss.getSource());
                holder2.comment_count2.setText("评论:" + newss.getComment_count());
                getImage(holder2.img_img2, newss.getMiddle_image());
                break;
            case 2:
                holder3.title3.setText(newss.getTitle());
                holder3.source3.setText("来源:" + newss.getSource());
                holder3.comment_count3.setText("评论:" + newss.getComment_count());
                List<String> img_url = newss.getImg_url();
                for (int i = 0; i < img_url.size(); i++) {
                    if (i == 0) {
                        getImage(holder3.img1, img_url.get(0));
                    } else if (i == 1) {
                        getImage(holder3.img2, img_url.get(1));
                    } else if (i == 2) {
                        getImage(holder3.img3, img_url.get(2));
                    }
                }
                break;
        }
        return convertView;
    }

    public void getImage(ImageView iv, String url) {
        ImageLoader loader = ImageLoader.getInstance();
        //配置初始参数
        loader.init(ImageLoaderConfiguration.createDefault(context));
        loader.displayImage(url, iv);
    }

    static class ViewHolder1 {
        public TextView title1;
        public TextView source1;
        public TextView comment_count1;

    }

    static class ViewHolder2 {
        public TextView title2;
        public TextView source2;
        public TextView comment_count2;
        public ImageView img_img2;

    }

    static class ViewHolder3 {
        public TextView title3;
        public TextView source3;
        public TextView comment_count3;
        public TextView time3;
        public ImageView img1;
        public ImageView img2;
        public ImageView img3;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值