效果圖
TabLayout+Fragment+RecyclerView+fresco+butterknife+greendao資料庫
TabLayout+Fragment+RecyclerView+fresco+butterknife+greendao資料庫 mvp+記憶體洩漏
package com.bawei.zhoukao1.mvp.view;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/13 08:37
*/
public interface MainView {
void sueccss(String data);
void fail(String error);
}
package com.bawei.zhoukao1.mvp.model;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/13 08:39
*/
public interface MainModel {
interface CallBackLister{
void sueccss(String data);
void fail(String error);
}
void getData(String url,CallBackLister callBackLister);
}
package com.bawei.zhoukao1.mvp.model;
import com.bawei.zhoukao1.mvp.utlis.OkHttpUtils;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/13 08:41
*/
public class MainModelIml implements MainModel {
//請求資料的方法
@Override
public void getData(String url, final CallBackLister callBackLister) {
new OkHttpUtils().get(url).reult(new OkHttpUtils.HttpLister() {
@Override
public void sueccss(String data) {
callBackLister.sueccss(data);
}
@Override
public void fail(String error) {
callBackLister.fail(error);
}
});
}
}
package com.bawei.zhoukao1.mvp.preseter;
import com.bawei.zhoukao1.mvp.model.MainModel;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/13 08:41
*/
public interface MainP {
void getData(String url);
}
package com.bawei.zhoukao1.mvp.preseter;
import com.bawei.zhoukao1.mvp.model.MainModel;
import com.bawei.zhoukao1.mvp.view.MainView;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/13 08:43
*/
public class MainPIml implements MainP, MainModel.CallBackLister {
private MainModel mainModel;
private MainView mainView;
public MainPIml(MainModel mainModel, MainView mainView){
this.mainModel=mainModel;
this.mainView=mainView;
}
@Override
public void getData(String url) {
mainModel.getData(url,this);
}
@Override
public void sueccss(String data) {
mainView.sueccss(data);
}
@Override
public void fail(String error) {
mainView.fail(error);
}
//防止記憶體洩漏(定義一個方法)
public void deteor(){
if (mainModel!=null){
mainModel=null;
}if(mainView!=null){
mainView=null;
}
System.gc();
}
}
package com.bawei.zhoukao1.mvp.utlis;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/13 08:39
*/
public class OkHttpUtils {
private HttpLister httpLister;
int HTTP_SUECCSS=200;
int HTTP_FAIL=201;
public OkHttpUtils get(String url){
doHttp(url,0,null);
return this;
}
public OkHttpUtils post(String url, FormBody. Builder boyebuilder){
doHttp(url,1,boyebuilder);
return this;
}
private void doHttp(String url,int type, FormBody. Builder boyebuilder) {
OkHttpClient client=new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request=chain.request();
Log.i("intercept ",request.url().host());
return chain.proceed(request);
}
}).build();
Request.Builder builder=new Request.Builder();
if (type==0){
builder.get();
}else {
builder.post(boyebuilder.build());
}
builder.url(url);
Request request=builder.build();
final Message message=Message.obtain();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
message.obj=e.getMessage();
message.what=HTTP_FAIL;
handler.sendMessage(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
message.obj=response.body().string();
message.what=HTTP_SUECCSS;
handler.sendMessage(message);
}
});
}
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==HTTP_SUECCSS){
String data= (String) msg.obj;
httpLister.sueccss(data);
}else {
String error= (String) msg.obj;
httpLister.fail(error);
}
}
};
//傳遞接口
public void reult(HttpLister httpLister){
this.httpLister=httpLister;
}
public interface HttpLister{
void sueccss(String data);
void fail(String error);
}
}
package com.bawei.zhoukao1.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.bawei.zhoukao1.R;
import com.bawei.zhoukao1.MyUtils;
import com.bawei.zhoukao1.bean.ResultBean;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/12 19:43
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MhV> {
private Context context;
private List<ResultBean> list = new ArrayList<>();
public MyAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public MhV onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.item_shop, null);
MhV mhV = new MhV(view);
return mhV;
}
@Override
public void onBindViewHolder(@NonNull MhV mhV, final int i) {
mhV.simple.setImageURI(list.get(i).getMasterPic());
mhV.tvTitle.setText(list .get(i).getCommodityName());
mhV.tvPrice.setText("¥"+list .get(i).getPrice());
//設定條目點選事件
mhV.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//儲存到資料庫
MyUtils.getResultUtils().add(list.get(i));
}
});
}
@Override
public int getItemCount() {
return list .size();
}
public void setList(List<ResultBean> list ) {
this.list = list ;
notifyDataSetChanged();
}
public class MhV extends RecyclerView.ViewHolder {
@BindView(R.id.simple)
SimpleDraweeView simple;
@BindView(R.id.tv_title)
TextView tvTitle;
@BindView(R.id.tv_price)
TextView tvPrice;
public MhV(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
}
}
}
App
package com.bawei.zhoukao1.app;
import android.app.Application;
import android.os.Environment;
import com.bawei.zhoukao1.MyUtils;
import com.facebook.cache.disk.DiskCacheConfig;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import java.io.File;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/12 19:03
*/
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
// Fresco.initialize(this);
// 進階初始化配置緩存檔案,:
Fresco.initialize(this, ImagePipelineConfig.newBuilder(MyApp.this)
.setMainDiskCacheConfig(
DiskCacheConfig.newBuilder(this)
.setBaseDirectoryPath(new File(Environment.getExternalStorageDirectory().getAbsolutePath()))
.setMaxCacheSize(10*1024*1024)//緩存大小
.build()
)
.build()
);
//初始化
MyUtils.getResultUtils().init(this);
}
}
fr
package com.bawei.zhoukao1.fr;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bawei.zhoukao1.MyUtils;
import com.bawei.zhoukao1.NetWork;
import com.bawei.zhoukao1.R;
import com.bawei.zhoukao1.adapter.MyAdapter;
import com.bawei.zhoukao1.bean.MyBean;
import com.bawei.zhoukao1.bean.ResultBean;
import com.bawei.zhoukao1.mvp.model.MainModelIml;
import com.bawei.zhoukao1.mvp.preseter.MainP;
import com.bawei.zhoukao1.mvp.preseter.MainPIml;
import com.bawei.zhoukao1.mvp.view.MainView;
import com.google.gson.Gson;
import java.io.IOException;
import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/12 19:08
*/
public class Fragment1 extends Fragment implements MainView {
// private String url = "http://172.17.8.100/small/commodity/v1/findCommodityByKeyword?page=1&count=10&keyword=電腦";
private MainPIml mainPIml;
private MyAdapter myAdapter;
@BindView(R.id.recycler)
RecyclerView recycler;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment1, null);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//擴充卡
myAdapter=new MyAdapter(getActivity());
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
recycler.setLayoutManager(gridLayoutManager);
recycler.setAdapter(myAdapter);
//請求資料
mainPIml=new MainPIml(new MainModelIml(),this);
doHttp();
}
private void doHttp() {
mainPIml.getData("http://172.17.8.100/small/commodity/v1/findCommodityByKeyword?page=1&count=10&keyword=女裝");
}
@Override
public void sueccss(String data) {
MyBean list = new Gson().fromJson(data, MyBean.class);
myAdapter.setList(list .getResult());
}
@Override
public void fail(String error) {
mainPIml.fail(error);
}
@Override
public void onDestroy() {
super.onDestroy();
mainPIml.deteor();
}
}
package com.bawei.zhoukao1.fr;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.bawei.zhoukao1.R;
import com.bawei.zhoukao1.MyUtils;
import com.bawei.zhoukao1.adapter.MyAdapter;
import com.bawei.zhoukao1.bean.ResultBean;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/12 19:08
*/
public class Fragment2 extends Fragment {
@BindView(R.id.recycler)
RecyclerView mRecycler;
private MyAdapter myAdapter;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=View.inflate(getActivity(), R.layout.fragment2,null);
ButterKnife.bind(this,view);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
myAdapter=new MyAdapter(getActivity());
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecycler.setLayoutManager(gridLayoutManager);
mRecycler.setAdapter(myAdapter);
}
@OnClick(R.id.btn_qure)
public void click() {
List<ResultBean> list = MyUtils.getResultUtils().loadAll();
//Toast.makeText(getActivity(),"dndnndn",Toast.LENGTH_SHORT).show();
myAdapter.setList(list);
}
}
bean
package com.bawei.zhoukao1.bean;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/12 19:47
*/
public class MyBean {
/**
* result : [{"commodityId":174,"commodityName":"帆布派 Canvas artisan 蘋果筆記本電腦包 女14/15.6寸惠普電腦包聯想1 PT38-1酒紅色 14寸可用","masterPic":"http://172.17.8.100/images/small/commodity/xbsd/dnb/5/1.jpg","price":229,"saleNum":0},{"commodityId":190,"commodityName":"XDDESIGN 蒙馬特城市安全防盜背包 經典版 商務男女款雙肩包15英寸筆記本電腦包 ","masterPic":"http://172.17.8.100/images/small/commodity/xbsd/sjb/7/1.jpg","price":369,"saleNum":0},{"commodityId":171,"commodityName":"HOTBOOM2018男士雙肩包休閑背包潮流學生書包多功能筆記本商務14英寸電腦包5606 時尚灰","masterPic":"http://172.17.8.100/images/small/commodity/xbsd/dnb/2/1.jpg","price":109,"saleNum":0},{"commodityId":187,"commodityName":"DiDe迪德雙肩包男士背包休閑大容量防水複古電腦包多功能時尚學生潮流書包 DQ860","masterPic":"http://172.17.8.100/images/small/commodity/xbsd/sjb/4/1.jpg","price":89,"saleNum":0},{"commodityId":176,"commodityName":"瑞士軍刀大容量背包多功能戶外出差旅行包雙肩包男15.6英寸筆記本電腦包手提斜挎行李包旅遊登山包防潑水 黑色【多功能手提斜跨雙肩單肩】","masterPic":"http://172.17.8.100/images/small/commodity/xbsd/dnb/7/1.jpg","price":119,"saleNum":0},{"commodityId":173,"commodityName":"新番祖 電腦包 13.3/14/15.6英寸蘋果筆記本電腦包 女 手提公文包 貝殼 櫻花粉-13.3英寸及以下(淑女款)","masterPic":"http://172.17.8.100/images/small/commodity/xbsd/dnb/4/1.jpg","price":119,"saleNum":0},{"commodityId":189,"commodityName":"高爾夫GOLF錦綸雙肩包男士個性旅行背包大容量電腦背包D8BV33913J","masterPic":"http://172.17.8.100/images/small/commodity/xbsd/sjb/6/1.jpg","price":179,"saleNum":0},{"commodityId":170,"commodityName":"愛登堡電腦包背包男士雙肩包14/15.6英寸防潑水大容量商務通勤旅行休閑學生筆記本書包 黑色643001-01","masterPic":"http://172.17.8.100/images/small/commodity/xbsd/dnb/1/1.jpg","price":99,"saleNum":0},{"commodityId":186,"commodityName":"SWISSGEAR雙肩包 防水多功能筆記本電腦包14.6英寸/15.6英寸商務背包男學生書包休閑 SA-9393III","masterPic":"http://172.17.8.100/images/small/commodity/xbsd/sjb/3/1.jpg","price":99,"saleNum":0},{"commodityId":175,"commodityName":"倍晶 蘋果微軟聯想惠普華碩戴爾宏基筆記本電腦包13英寸内膽14手提15單肩15.6小新11保護套 粉紅色手提包+同色電源小包 15.6英寸","masterPic":"http://172.17.8.100/images/small/commodity/xbsd/dnb/6/1.jpg","price":119,"saleNum":0}]
* message : 查詢成功
* status : 0000
*/
private String message;
private String status;
private List<ResultBean> result;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
}
package com.bawei.zhoukao1.bean;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/12 20:26
*/
@Entity
public class ResultBean {
@Id(autoincrement = true)
private Long id;
private int commodityId;
private String commodityName;
private String masterPic;
private int price;
private int saleNum;
@Generated(hash = 1376070191)
public ResultBean(Long id, int commodityId, String commodityName,
String masterPic, int price, int saleNum) {
this.id = id;
this.commodityId = commodityId;
this.commodityName = commodityName;
this.masterPic = masterPic;
this.price = price;
this.saleNum = saleNum;
}
@Generated(hash = 2137771703)
public ResultBean() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public int getCommodityId() {
return this.commodityId;
}
public void setCommodityId(int commodityId) {
this.commodityId = commodityId;
}
public String getCommodityName() {
return this.commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public String getMasterPic() {
return this.masterPic;
}
public void setMasterPic(String masterPic) {
this.masterPic = masterPic;
}
public int getPrice() {
return this.price;
}
public void setPrice(int price) {
this.price = price;
}
public int getSaleNum() {
return this.saleNum;
}
public void setSaleNum(int saleNum) {
this.saleNum = saleNum;
}
}
操作資料庫的工具類 MyUtils
package com.bawei.zhoukao1;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.bawei.zhoukao1.bean.ResultBean;
import java.util.ArrayList;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/12 20:26
*/
public class MyUtils {
private ResultBeanDao dao;
private MyUtils() {
}
private static MyUtils myUtils = null;
public static MyUtils getResultUtils() {
synchronized (MyUtils.class) {
if (myUtils == null) {
myUtils = new MyUtils();
}
}
return myUtils ;
}
//初始化
public void init(Context context) {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "news");
SQLiteDatabase dp = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(dp);
dao = daoMaster.newSession().getResultBeanDao();
}
//添加
public void add(ResultBean bean) {
List<ResultBean> list = dao.loadAll();
boolean isK=false;
for (int i = 0; i < list.size(); i++) {
ResultBean beans = list.get(i);
if (bean.getCommodityId() == beans .getCommodityId()) {
isK=true;
dao.update(bean);
}
}
if (!isK){
dao.insert(bean);
}
}
//查詢全部
public List<ResultBean> loadAll() {
//定義一個大集合
List<ResultBean> lists=new ArrayList<>();
List<ResultBean> list = dao.loadAll();
for (int i = 0; i <list.size() ; i++) {
ResultBean bean= list.get(i);
lists.add(0,bean);
}
return lists;
}
}
自動生成的三個類
package com.bawei.zhoukao1;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;
import org.greenrobot.greendao.AbstractDaoMaster;
import org.greenrobot.greendao.database.StandardDatabase;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseOpenHelper;
import org.greenrobot.greendao.identityscope.IdentityScopeType;
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/**
* Master of DAO (schema version 1): knows all DAOs.
*/
public class DaoMaster extends AbstractDaoMaster {
public static final int SCHEMA_VERSION = 1;
/** Creates underlying database table using DAOs. */
public static void createAllTables(Database db, boolean ifNotExists) {
ResultBeanDao.createTable(db, ifNotExists);
}
/** Drops underlying database table using DAOs. */
public static void dropAllTables(Database db, boolean ifExists) {
ResultBeanDao.dropTable(db, ifExists);
}
/**
* WARNING: Drops all table on Upgrade! Use only during development.
* Convenience method using a {@link DevOpenHelper}.
*/
public static DaoSession newDevSession(Context context, String name) {
Database db = new DevOpenHelper(context, name).getWritableDb();
DaoMaster daoMaster = new DaoMaster(db);
return daoMaster.newSession();
}
public DaoMaster(SQLiteDatabase db) {
this(new StandardDatabase(db));
}
public DaoMaster(Database db) {
super(db, SCHEMA_VERSION);
registerDaoClass(ResultBeanDao.class);
}
public DaoSession newSession() {
return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
}
public DaoSession newSession(IdentityScopeType type) {
return new DaoSession(db, type, daoConfigMap);
}
/**
* Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} -
*/
public static abstract class OpenHelper extends DatabaseOpenHelper {
public OpenHelper(Context context, String name) {
super(context, name, SCHEMA_VERSION);
}
public OpenHelper(Context context, String name, CursorFactory factory) {
super(context, name, factory, SCHEMA_VERSION);
}
@Override
public void onCreate(Database db) {
Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION);
createAllTables(db, false);
}
}
/** WARNING: Drops all table on Upgrade! Use only during development. */
public static class DevOpenHelper extends OpenHelper {
public DevOpenHelper(Context context, String name) {
super(context, name);
}
public DevOpenHelper(Context context, String name, CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
dropAllTables(db, true);
onCreate(db);
}
}
}
package com.bawei.zhoukao1;
import java.util.Map;
import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.AbstractDaoSession;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.identityscope.IdentityScopeType;
import org.greenrobot.greendao.internal.DaoConfig;
import com.bawei.zhoukao1.bean.ResultBean;
import com.bawei.zhoukao1.ResultBeanDao;
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/**
* {@inheritDoc}
*
* @see org.greenrobot.greendao.AbstractDaoSession
*/
public class DaoSession extends AbstractDaoSession {
private final DaoConfig resultBeanDaoConfig;
private final ResultBeanDao resultBeanDao;
public DaoSession(Database db, IdentityScopeType type, Map<Class<? extends AbstractDao<?, ?>>, DaoConfig>
daoConfigMap) {
super(db);
resultBeanDaoConfig = daoConfigMap.get(ResultBeanDao.class).clone();
resultBeanDaoConfig.initIdentityScope(type);
resultBeanDao = new ResultBeanDao(resultBeanDaoConfig, this);
registerDao(ResultBean.class, resultBeanDao);
}
public void clear() {
resultBeanDaoConfig.clearIdentityScope();
}
public ResultBeanDao getResultBeanDao() {
return resultBeanDao;
}
}
package com.bawei.zhoukao1;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;
import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import com.bawei.zhoukao1.bean.ResultBean;
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/**
* DAO for table "RESULT_BEAN".
*/
public class ResultBeanDao extends AbstractDao<ResultBean, Long> {
public static final String TABLENAME = "RESULT_BEAN";
/**
* Properties of entity ResultBean.<br/>
* Can be used for QueryBuilder and for referencing column names.
*/
public static class Properties {
public final static Property Id = new Property(0, Long.class, "id", true, "_id");
public final static Property CommodityId = new Property(1, int.class, "commodityId", false, "COMMODITY_ID");
public final static Property CommodityName = new Property(2, String.class, "commodityName", false, "COMMODITY_NAME");
public final static Property MasterPic = new Property(3, String.class, "masterPic", false, "MASTER_PIC");
public final static Property Price = new Property(4, int.class, "price", false, "PRICE");
public final static Property SaleNum = new Property(5, int.class, "saleNum", false, "SALE_NUM");
}
public ResultBeanDao(DaoConfig config) {
super(config);
}
public ResultBeanDao(DaoConfig config, DaoSession daoSession) {
super(config, daoSession);
}
/** Creates the underlying database table. */
public static void createTable(Database db, boolean ifNotExists) {
String constraint = ifNotExists? "IF NOT EXISTS ": "";
db.execSQL("CREATE TABLE " + constraint + "\"RESULT_BEAN\" (" + //
"\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
"\"COMMODITY_ID\" INTEGER NOT NULL ," + // 1: commodityId
"\"COMMODITY_NAME\" TEXT," + // 2: commodityName
"\"MASTER_PIC\" TEXT," + // 3: masterPic
"\"PRICE\" INTEGER NOT NULL ," + // 4: price
"\"SALE_NUM\" INTEGER NOT NULL );"); // 5: saleNum
}
/** Drops the underlying database table. */
public static void dropTable(Database db, boolean ifExists) {
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"RESULT_BEAN\"";
db.execSQL(sql);
}
@Override
protected final void bindValues(DatabaseStatement stmt, ResultBean entity) {
stmt.clearBindings();
Long id = entity.getId();
if (id != null) {
stmt.bindLong(1, id);
}
stmt.bindLong(2, entity.getCommodityId());
String commodityName = entity.getCommodityName();
if (commodityName != null) {
stmt.bindString(3, commodityName);
}
String masterPic = entity.getMasterPic();
if (masterPic != null) {
stmt.bindString(4, masterPic);
}
stmt.bindLong(5, entity.getPrice());
stmt.bindLong(6, entity.getSaleNum());
}
@Override
protected final void bindValues(SQLiteStatement stmt, ResultBean entity) {
stmt.clearBindings();
Long id = entity.getId();
if (id != null) {
stmt.bindLong(1, id);
}
stmt.bindLong(2, entity.getCommodityId());
String commodityName = entity.getCommodityName();
if (commodityName != null) {
stmt.bindString(3, commodityName);
}
String masterPic = entity.getMasterPic();
if (masterPic != null) {
stmt.bindString(4, masterPic);
}
stmt.bindLong(5, entity.getPrice());
stmt.bindLong(6, entity.getSaleNum());
}
@Override
public Long readKey(Cursor cursor, int offset) {
return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
}
@Override
public ResultBean readEntity(Cursor cursor, int offset) {
ResultBean entity = new ResultBean( //
cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
cursor.getInt(offset + 1), // commodityId
cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // commodityName
cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // masterPic
cursor.getInt(offset + 4), // price
cursor.getInt(offset + 5) // saleNum
);
return entity;
}
@Override
public void readEntity(Cursor cursor, ResultBean entity, int offset) {
entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
entity.setCommodityId(cursor.getInt(offset + 1));
entity.setCommodityName(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
entity.setMasterPic(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
entity.setPrice(cursor.getInt(offset + 4));
entity.setSaleNum(cursor.getInt(offset + 5));
}
@Override
protected final Long updateKeyAfterInsert(ResultBean entity, long rowId) {
entity.setId(rowId);
return rowId;
}
@Override
public Long getKey(ResultBean entity) {
if(entity != null) {
return entity.getId();
} else {
return null;
}
}
@Override
public boolean hasKey(ResultBean entity) {
return entity.getId() != null;
}
@Override
protected final boolean isEntityUpdateable() {
return true;
}
}
MainActivity
package com.bawei.zhoukao1;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import com.bawei.zhoukao1.fr.Fragment1;
import com.bawei.zhoukao1.fr.Fragment2;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.viewpager)
ViewPager viewpager;
@BindView(R.id.tablayout)
TabLayout tablayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
FragmentAdapter fragmentAdapter=new FragmentAdapter(getSupportFragmentManager());
viewpager.setAdapter(fragmentAdapter);
tablayout.setupWithViewPager(viewpager);
}
private class FragmentAdapter extends FragmentPagerAdapter{
private String[] mTitle = {
"首頁", "記錄"
};
private List<Fragment> fragmentList=new ArrayList<>();
public FragmentAdapter(FragmentManager fm) {
super(fm);
fragmentList.add(new Fragment1());
fragmentList.add(new Fragment2());
}
@Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mTitle[position];
}
}
}
布局MainActivity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_above="@+id/tablayout"
android:id="@+id/viewpager"/>
<android.support.design.widget.TabLayout
android:layout_height="50dp"
android:layout_width="match_parent"
android:id="@+id/tablayout"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
fragment1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler"
/>
</RelativeLayout>
fragment2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btn_qure"
android:text="查詢記錄"/>
<android.support.v7.widget.RecyclerView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/recycler"
android:layout_marginTop="10dp"
android:layout_below="@+id/btn_qure"/>
</RelativeLayout>
item_shop
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/simple"
android:layout_width="200dp"
android:layout_height="200dp" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#d43c3c" />
</LinearLayout>
</RelativeLayout>