展示数据的适配器

package com.example.zh.week2.view.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zh.week2.R;
import com.example.zh.week2.model.bean.ZhanShiBean;

import java.util.List;

public class ZhanShiAdapter extends RecyclerView.Adapter<ZhanShiAdapter.VVH> {
Context context;
List<ZhanShiBean.NewslistBean> beans;
LayoutInflater layoutInflater;

public ZhanShiAdapter(Context context, List<ZhanShiBean.NewslistBean> beans) {
    this.layoutInflater = LayoutInflater.from(context);
    this.context = context;
    this.beans = beans;
}

@NonNull
@Override
public ZhanShiAdapter.VVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = layoutInflater.inflate(R.layout.reseceview, viewGroup, false);
    VVH vvh = new VVH(view);
    return vvh;
}

@Override
public void onBindViewHolder(@NonNull ZhanShiAdapter.VVH vvh, int i) {
    vvh.text.setText(beans.get(i).getTitle());
    Glide.with(context).load(beans.get(i).getPicUrl()).into(vvh.img);
}

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

public class VVH extends RecyclerView.ViewHolder {

    private final ImageView img;
    private final TextView text;

    public VVH(@NonNull View itemView) {
        super(itemView);
        img = itemView.findViewById(R.id.img);
        text = itemView.findViewById(R.id.text);
    }
}

}

public class ZSPresenter {

private final Okhttp getdanli;
ZhanShiActivity zhyanShi;

public ZSPresenter(ZhanShiActivity zhyanShi1) {
    getdanli = Okhttp.getdanli();
    this.zhyanShi = zhyanShi1;
}
public void getzcpresenter(String url){
    getdanli.get(url, new Okhttp.getokhttp() {
        @Override
        public void getcheng(String s) {
            zhyanShi.getview(s);
        }
    });
}

}

public class ZhanShiActivity extends AppCompatActivity implements IManintenter {

private RecyclerView recyclerView;
private String path = "http://api.tianapi.com/meinv/?key=11500f2566e70105ee1c28a12f5f750a";
Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what == 1) {
            String s = (String) msg.obj;
                    inten(s);
        }
    }
};

private void inten(String s) {
    Gson gson = new Gson();
    ZhanShiBean zhanShiBean = gson.fromJson(s, ZhanShiBean.class);
    List<ZhanShiBean.NewslistBean> newslist = zhanShiBean.getNewslist();
    LinearLayoutManager layoutManager = new LinearLayoutManager(ZhanShiActivity.this);
    layoutManager.setOrientation(OrientationHelper.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);
    ZhanShiAdapter zhanShiAdapter = new ZhanShiAdapter(ZhanShiActivity.this, newslist);
    recyclerView.setAdapter(zhanShiAdapter);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_zhyan_shi);
    recyclerView = findViewById(R.id.recyclerView);
    ZSPresenter zsPresenter = new ZSPresenter(this);
    zsPresenter.getzcpresenter(path);
}

@Override
public void getview(String s) {
    Log.i("AA", "展示" + s);
    Message message = new Message();
    message.what = 1;
    message.obj = s;
    handler.sendMessage(message);
}

}
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值