我的第一个应用程序需要一些帮助。我制作了一个旅行指南应用程序,它使用改进的API从JSON中提取信息和图像链接。
我的应用程序在线工作很棒,提取所有数据,但现在我希望用户能够在缓存后离线查看所有信息。
客户端API
public static final String BASE_URL = "https://quarkbackend.com";
public static Retrofit retrofit = null;
public static Retrofit getClient(){
if (retrofit==null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
服务API
public interface Service {
@GET("/getfile/beyond-infinity-gd/homepage")
Call<AlbumsResponse> getHomepage();
@GET("/getfile/beyond-infinity-gd/tours")
Call<AlbumsResponse> getTours();
...
适配器
public class AlbumsAdapter extends RecyclerView.Adapter<AlbumsAdapter.MyViewHolder> {
private Context mContext;
private List<Album> albumList;
public AlbumsAdapter(Context mContext, List<Album> albumList) {
this.mContext = mContext;
this.albumList = albumList;
}
@Override
public AlbumsAdapter.MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.album_card, viewGroup, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final AlbumsAdapter.MyViewHolder viewHolder, int i) {
viewHolder.title.setText(albumList.get(i).getTitle());
viewHolder.count.setText(albumList.get(i).getSubtitle());
//load album cover using picasso
Picasso.with(mContext)
.load(albumList.get(i).getThumbnail())
.placeholder(R.drawable.load)
.into(viewHolder.thumbnail);
}
@Override
public int getItemCount() {
return albumList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, count;
public ImageView thumbnail;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
count = (TextView) view.findViewById(R.id.count);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
//on item click
itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
int pos = getAdapterPosition();
if (pos != RecyclerView.NO_POSITION){
Album clickedDataItem = albumList.get(pos);
Intent intent = new Intent(mContext, DetailActivity.class);
intent.putExtra("title", albumList.get(pos).getTitle());
intent.putExtra("subtitle", albumList.get(pos).getSubtitle());
intent.putExtra("body", albumList.get(pos).getBody());
intent.putExtra("thumbnail", albumList.get(pos).getThumbnail());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
}
});
}
}