RetroFitDemo小练习
MainActivity public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private List<Category> list; private CategoryAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.myrecycleview); list = new ArrayList<>(); recyclerView.setLayoutManager(new LinearLayoutManager(this)); adapter = new CategoryAdapter(this, list); recyclerView.setAdapter(adapter); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://www.zhaoapi.cn/") .addConverterFactory(GsonConverterFactory.create()) .build(); ApiSevice service = retrofit.create(ApiSevice.class); Call<MessageBean<List<Category>>> call = service.getCatagory(); call.enqueue(new Callback<MessageBean<List<Category>>>() { @Override public void onResponse(Call<MessageBean<List<Category>>> call, Response<MessageBean<List<Category>>> response) { MessageBean<List<Category>> message = response.body(); if (message.getData() != null) { list.clear(); list.addAll(message.getData()); adapter.notifyDataSetChanged(); } } @Override public void onFailure(Call<MessageBean<List<Category>>> call, Throwable t) { Toast.makeText(MainActivity.this, "网络请求失败,原因是:"+t.getMessage(), Toast.LENGTH_SHORT).show(); } }); } }
Api类 public interface ApiSevice { @GET("product/getCatagory") Call<MessageBean<List<Category>>> getCatagory(); }
bean 类 @Entity public class Category { @Id private int cid; private String createtime; private String icon; private int ishome; private String name; @Generated(hash = 1577840102) public Category(int cid, String createtime, String icon, int ishome, String name) { this.cid = cid; this.createtime = createtime; this.icon = icon; this.ishome = ishome; this.name = name; } @Generated(hash = 1150634039) public Category() { } public int getCid() { return this.cid; } public void setCid(int cid) { this.cid = cid; } public String getCreatetime() { return this.createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getIcon() { return this.icon; } public void setIcon(String icon) { this.icon = icon; } public int getIshome() { return this.ishome; } public void setIshome(int ishome) { this.ishome = ishome; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } }
MessageBean public class MessageBean<T> { private String msg; private String code; private T data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public T getData() { return data; } public void setData(T data) { this.data = data; } }
Adapter (重点!!!) public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> { private Context context; private List<Category> list; public CategoryAdapter(Context context, List<Category> list) { this.context = context; this.list = list; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.activity_item, null); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { Glide.with(context).load(list.get(position).getIcon()).into(holder.imgLogo); holder.txtName.setText(list.get(position).getName()); holder.txtTime.setText(list.get(position).getCreatetime()); } @Override public int getItemCount() { return list.size(); } class ViewHolder extends RecyclerView.ViewHolder{ private ImageView imgLogo; private TextView txtName; private TextView txtTime; public ViewHolder(View itemView) { super(itemView); imgLogo = itemView.findViewById(R.id.myImage); txtName = itemView.findViewById(R.id.mytitle); txtTime = itemView.findViewById(R.id.mytime); } } }