目录
???????????????????运用到数据库(GreenDao)
以及用到适配器的MyAdapter类
最后的Activity类
文中用到的 APP类
单例的 数据库的Manager
用来传给适配器的item.xml
activity.xml
?
运用到数据库(GreenDao)
? ? ? ? 先创建一个这样的prosen类
@Entity
public class Search {
@Id(autoincrement = true)
private Long id;
private String name;
}
自定义一个继承RecyclerView的类
public class MyRecycle extends RecyclerView {
private boolean isFull = false;
public MyRecycle(@NonNull Context context) {
super(context);
}
public MyRecycle(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override //100
protected void onMeasure(int widthSpec, int heightSpec) {
super.onMeasure(widthSpec, heightSpec);
// int mode = MeasureSpec.getMode(100);
// int size = MeasureSpec.getSize(100);
// MeasureSpec.EXACTLY;//
// MeasureSpec.AT_MOST;//
//默认高,展开高
if(isFull){
int i = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthSpec, i);
}else{
setMeasuredDimension(widthSpec,400);
}
}
public boolean isFull() {
return isFull;
}
public void setFull(boolean full) {
isFull = full;
requestLayout();
}
}
以及用到适配器的MyAdapter类
public class MyAdapter extends BaseQuickAdapter<Search, BaseViewHolder> {
@Override
protected void convert(BaseViewHolder helper, Search item) {
TextView name = helper.getView(R.id.name);
name.setText(item.getName());
helper.addOnClickListener(R.id.del_img);
}
public MyAdapter(int layoutResId, @Nullable List<Search> data) {
super(layoutResId, data);
}
}
最后的Activity类
public class MainActivity extends AppCompatActivity {
private ScrollView scrollView;
private LinearLayout ll;
private EditText edit;
private ImageView search;
private MyRecycle myRecycle;
private TextView showAll;
private List<Search> list;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (ScrollView) findViewById(R.id.scrollView);
ll = (LinearLayout) findViewById(R.id.ll);
edit = (EditText) findViewById(R.id.edit);
search = (ImageView) findViewById(R.id.search);
myRecycle = (MyRecycle) findViewById(R.id.myRecycle);
showAll = (TextView) findViewById(R.id.showAll);
list = new ArrayList<>();
// Search search = new Search();
// search.setName("哈哈哈");
//
// list.add(search);
// list.add(search);
// list.add(search);
// list.add(search);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this){
@Override
public boolean canScrollVertically() {
return false;
}
};
myRecycle.setLayoutManager(linearLayoutManager);
//从数据库先查询一次
SearchDao searchDao = DaoManager.getInstance().getDaoSession().getSearchDao();
List<Search> list1 = searchDao.queryBuilder().orderDesc(SearchDao.Properties.Id).limit(10).build().list();
list.addAll(list1);
myAdapter = new MyAdapter(R.layout.rv_layout, list);
myRecycle.setAdapter(myAdapter);
myAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
Search item = myAdapter.getItem(position);
//1
list.remove(item);
//2
DaoManager.getInstance().getDaoSession().getSearchDao().delete(item);
//3
myAdapter.notifyDataSetChanged();
}
});
}
public void add(View view) {
String string = edit.getText().toString();
Search search = new Search();
search.setName(string);
// search.setId(1l);
//1,到集合
// list.add(search);
//排序
// Collections.sort(list, new Comparator<Search>() {
// @Override
// public int compare(Search o1, Search o2) { // 0 >0 <0
// return (int) (o2.getId() - o1.getId());
// }
// });
list.clear();
//2,到数据库
SearchDao searchDao = DaoManager.getInstance().getDaoSession().getSearchDao();
searchDao.insert(search);
List<Search> list1 = searchDao.queryBuilder().orderDesc(SearchDao.Properties.Id).limit(10).build().list();
list.addAll(list1);
//适配器刷新一下
myAdapter.notifyDataSetChanged();
}
public void show(View view) {
if(myRecycle.isFull()){
//
showAll.setText("展开所有");
myRecycle.setFull(false);
new AlertDialog.Builder(this).setTitle("删除").setMessage("确定吗?")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DaoManager.getInstance().getDaoSession().getSearchDao().deleteAll();
list.clear();
myAdapter.notifyDataSetChanged();
}
}).show();
}else{
showAll.setText("删除所有");
myRecycle.setFull(true);
}
}
}
文中用到的 APP类
public class App extends Application {
public static Context context;
@Override
public void onCreate() {
super.onCreate();
context = this;
}
}
单例的 数据库的Manager
public class DaoManager {
private DaoSession daoSession;
private static DaoManager daoManger;
private DaoManager(){
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(App.context, "search.db");
SQLiteDatabase writableDatabase = devOpenHelper.getWritableDatabase();//
DaoMaster daoMaster = new DaoMaster(writableDatabase);
daoSession = daoMaster.newSession();
}
public static DaoManager getInstance(){
if(daoManger == null){
synchronized (DaoManager.class){
if(daoManger == null){
daoManger = new DaoManager();
}
}
}
return daoManger;
}
public DaoSession getDaoSession() {
return daoSession;
}
}
用来传给适配器的item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:text="123"
></TextView>
<ImageView
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_marginRight="@dimen/dp_72"
android:id="@+id/del_img"></ImageView>
</RelativeLayout>
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:id="@+id/ll">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="56dp">
<EditText
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:id="@+id/edit"></EditText>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="add"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:id="@+id/search"></ImageView>
</LinearLayout>
<com.fenghongzhang.day014.MyRecycle
android:layout_width="match_parent"
android:id="@+id/myRecycle"
android:layout_height="wrap_content">
</com.fenghongzhang.day014.MyRecycle>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="展示所有"
android:id="@+id/showAll"
android:onClick="show"
></TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:src="@mipmap/ic_launcher"></ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:src="@mipmap/ic_launcher"></ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:src="@mipmap/ic_launcher"></ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:src="@mipmap/ic_launcher"></ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:src="@mipmap/ic_launcher"></ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:src="@mipmap/ic_launcher"></ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:src="@mipmap/ic_launcher"></ImageView><ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:src="@mipmap/ic_launcher"></ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:src="@mipmap/ic_launcher"></ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:src="@mipmap/ic_launcher"></ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:src="@mipmap/ic_launcher"></ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
|