依賴數據庫
retrofit依賴
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
greendao依賴
compile'org.greenrobot:greendao:3.0.1'
compile'org.greenrobot:greendao-generator:3.0.0'
butterknife依賴
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
GankApi接口
public interface GankApi {
@GET("api/data/Android/{size}/{page}")
Call<GankData<GankFeed>> list(@Path("size") int size,@Path("page") int page);
}
Bean包
GankData類
public class GankData <T>{
public boolean error;
public List<T> results;
@Override
public String toString() {
return "GankData{" +
"error=" + error +
", results=" + results +
'}';
}
}
GankFeed類
@Entity
public class GankFeed {
public String _id;
public String createdAt;
public String desc;
public String publishedAt;
public String source;
public String type;
public String url;
public boolean used;
public String who;
@Convert(converter = ImageTypeConverter.class,columnType = String.class)
public List<String> images;
@Generated(hash = 632180549)
public GankFeed(String _id, String createdAt, String desc, String publishedAt,
String source, String type, String url, boolean used, String who,
List<String> images) {
this._id = _id;
this.createdAt = createdAt;
this.desc = desc;
this.publishedAt = publishedAt;
this.source = source;
this.type = type;
this.url = url;
this.used = used;
this.who = who;
this.images = images;
}
@Generated(hash = 1242866017)
public GankFeed() {
}
若是接口數據中出現有的對象數據有的爲空有的不爲空還要向數據庫中存的話定義下邊的類 這個類須要在 GankFeed中該屬性上寫@Convert(converter(原始數據) = ImageTypeConverter.class,columnType(轉換的類型) = String.class)
public class ImageTypeConverter implements PropertyConverter<List<String>, String> {
@Override
public List<String> convertToEntityProperty(String databaseValue) {
ArrayList<String> list = new ArrayList<>();
list.add(databaseValue);
return list;
}
@Override
public String convertToDatabaseValue(List<String> entityProperty) {
return (entityProperty != null && !entityProperty.isEmpty()) ? "" : entityProperty.get(0);
}
}
MainActivity類
public class MainActivity extends AppCompatActivity {
private List<GankFeed> list1 = new ArrayList<>();
@BindView(R.id.list)
ListView list;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
// listView= (ListView) findViewById(R.id.list);
// final ListView list = null;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://gank.io/")
.addConverterFactory(GsonConverterFactory.create())
.build();
GankApi gankApi = retrofit.create(GankApi.class);
Call<GankData<GankFeed>> gankDataCall = gankApi.list(10, 1);
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db");
Database db = helper.getWritableDb();
final DaoSession daoSession = new DaoMaster(db).newSession();
gankDataCall.enqueue(new Callback<GankData<GankFeed>>() {
@Override
public void onResponse(Call<GankData<GankFeed>> call, Response<GankData<GankFeed>> response) {
// Log.i(getLocalClassName(),response.body().toString());
GankFeedDao gankFeedDao = daoSession.getGankFeedDao();
list1 = response.body().results;
for (GankFeed feed : response.body().results) {
gankFeedDao.insert(feed);
}
list.setAdapter(new MyAdapter(MainActivity.this, list1));
//Log.d(getLocalClassName()+"數量:",gankFeedDao.count()+"");
// Log.d(getLocalClassName(),gankFeedDao.queryBuilder().build().list().toString());
}
@Override
public void onFailure(Call<GankData<GankFeed>> call, Throwable t) {
}
});
}
}
MyAdapter類
public class MyAdapter extends BaseAdapter { private Context context; private List<GankFeed> list; public MyAdapter(Context context, List<GankFeed> list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder = null; if(view==null){ holder=new ViewHolder(); view=View.inflate(context,R.layout.item,null); holder.tv=view.findViewById(R.id.tv); viewGroup.setTag(holder); }else { holder= (ViewHolder) viewGroup.getTag(); } holder.tv.setText(list.get(i).type); return view; } class ViewHolder{ TextView tv; }}