笔记

一、配置

1.修改gradle文件夹下的gradle-wrapper.properties修改为:

distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

2、在工程的buildgradle中导入

dependencies {

classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'

}

3、在项目的buildgradle中导入

//导入插件

apply plugin: 'com.android.application'

apply plugin: 'org.greenrobot.greendao'

//在dependencies中添加如下代码

dependencies {

compile 'org.greenrobot:greendao:3.2.2'

}

二、工具类

public classDaosingleton {private staticDaosingletondaosingleton;privateDaosingleton() {    }publicBookDao getdao(Context context) {        DaoMaster.DevOpenHelper devOpenHelper =newDaoMaster.DevOpenHelper(context,"students.db",null);        DaoMaster daoMaster =newDaoMaster(devOpenHelper.getReadableDb());        DaoSession daoSession = daoMaster.newSession();        BookDao studentsDao = daoSession.getBookDao();returnstudentsDao;    }public staticDaosingleton getDaosingleton() {if(daosingleton==null) {synchronized(Daosingleton.class) {if(daosingleton==null) {daosingleton=newDaosingleton();                }            }        }returndaosingleton;    }}

三、简单使用

public classMain2ActivityextendsAppCompatActivityimplementsView.OnClickListener, AdapterView.OnItemLongClickListener {privateButtoninsert;privateProgressBarbar;privateListViewlistview;privateBookDaogetdao;privateMyAdaptermyAdapter;privateListlist=null;privatePopupWindowwindow;@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main2);getdao= Daosingleton.getDaosingleton().getdao(Main2Activity.this);        initView();        initData();    }private voidinitPro(finalList list1) {        Handler handler=newHandler();        handler.postDelayed(newRunnable() {@Overridepublic voidrun() {                runOnUiThread(newRunnable() {@Overridepublic voidrun() {bar.setVisibility(View.GONE);myAdapter.notifys(list1);                    }                });            }        },2000);    }private voidinitView() {insert= (Button) findViewById(R.id.insert);bar= (ProgressBar) findViewById(R.id.bar);listview= (ListView) findViewById(R.id.listview);/* initPro();*/insert.setOnClickListener(this);    }private voidinitData() {list=getdao.queryBuilder().build().list();myAdapter=newMyAdapter(Main2Activity.this,list);listview.setAdapter(myAdapter);myAdapter.notifys(list);listview.setOnItemLongClickListener(this);    }@Overridepublic voidonClick(View v) {switch(v.getId()) {caseR.id.insert:                Intent intent=newIntent(Main2Activity.this,Main3Activity.class);                startActivity(intent);break;        }    }@Overridepublic booleanonItemLongClick(AdapterView adapterView, View view,final inti,longl) {        View view1=View.inflate(Main2Activity.this,R.layout.view,null);window=newPopupWindow(view1,300,300);window.showAsDropDown(listview.getChildAt(i));window.setBackgroundDrawable(newColorDrawable(0));window.setOutsideTouchable(true);        Button updata=(Button) view1.findViewById(R.id.updata);        Button delete=(Button) view1.findViewById(R.id.delete);        delete.setOnClickListener(newView.OnClickListener() {@Overridepublic voidonClick(View view) {                Book book =list.get(i);myAdapter.de(book);getdao.delete(book);myAdapter.notifys(list);window.dismiss();            }        });        updata.setOnClickListener(newView.OnClickListener() {@Overridepublic voidonClick(View view) {                Book book =list.get(i);getdao.delete(book);myAdapter.de(book);                Intent intent=newIntent(Main2Activity.this,Main3Activity.class);                startActivity(intent);window.dismiss();            }        });return false;    }@Overrideprotected voidonResume() {super.onResume();list.clear();list=getdao.queryBuilder().list();        initPro(list);    }}

4、

public classMyAdapterextendsBaseAdapter {privateContextcontext;privateListlist=newArrayList<>();privateViewHolderholder;privateBeanbean;publicMyAdapter(Context context, List list) {this.context= context;this.list= list;    }@Overridepublic intgetCount() {returnlist.size();    }@OverridepublicObject getItem(intposition) {returnlist.get(position);    }@Overridepublic longgetItemId(intposition) {returnposition;    }@OverridepublicView getView(intposition, View convertView, ViewGroup parent) {if(convertView ==null) {holder=newViewHolder();            convertView = View.inflate(context, R.layout.item,null);holder.name= (TextView) convertView.findViewById(R.id.name);holder.company= (TextView) convertView.findViewById(R.id.company);            convertView.setTag(holder);        }else{holder= (ViewHolder) convertView.getTag();        }bean=list.get(position);holder.name.setText(bean.getName());holder.company.setText(bean.getCompany());returnconvertView;    }static classViewHolder {        TextViewname;        TextViewcompany;    }public voidnotifys(List list1){this.list= list1;        notifyDataSetChanged();    }}

5、

public class MyAdapter extends RecyclerView.Adapter {

private final Context context;

private List list=new ArrayList<>();

private Listener listener;

public MyAdapter(Context context, List list3) {

this.context=context;

this.list=list3;

}

@Override

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view=View.inflate(context,R.layout.item,null);

MyAdapter.ViewHordle holder=new MyAdapter.ViewHordle(view);

return holder;

}

@Override

public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

MyAdapter.ViewHordle holder1= (MyAdapter.ViewHordle) holder;

holder1.name.setText(list.get(position).getName());

Glide.with(context)

.load(list.get(position).getFigure())

.error(R.drawable.d)

.into(holder1.tu);

holder.itemView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

listener.click(v,position);

}

});

}

@Override

public int getItemCount() {

return list.size();

}

class ViewHordle extends RecyclerView.ViewHolder{

ImageView tu;

TextView name;

public ViewHordle(View itemView) {

super(itemView);

tu= (ImageView) itemView.findViewById(R.id.images);

name= (TextView) itemView.findViewById(R.id.name);

}

}

public  void setOnItemClickListener(Listener listener){

this.listener=listener;

}

interface Listener{

void click(View v,int position);

}

public void notifys(List list){

notifyDataSetChanged();

}

}

你可能感兴趣的:(笔记)