GreenDao实现搜索历史记录及删除

APP配置:

apply plugin: 'org.greenrobot.greendao' // apply plugin

compile 'org.greenrobot:greendao:3.2.2' // add library

greendao {
    schemaVersion 2//版本号
    daoPackage 'com.example.administrator.xinzhengwei20180407n.gen'//一般写包名.gen
    targetGenDir 'src/main/java'
}

project配置:

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



bean类(注意写好参数Build - Make-project一下 生成gen目录):

@Entity//指定表名,允许删除修改
public class Users {
    
  private String name;

@Generated(hash = 230484915)
public Users(String name) {
    this.name = name;
}

@Generated(hash = 2146996206)
public Users() {
}

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

}
 
  

工具类:

public class GreenDaoUtils {
    private volatile static GreenDaoUtils greenDaoUtils;
    private DaoSession daoSession;
    private SQLiteDatabase database;

    private GreenDaoUtils(){

    }
    public static GreenDaoUtils getmInstance(){
        if (greenDaoUtils==null){
            synchronized (GreenDaoUtils.class){
                if (greenDaoUtils==null){
                    greenDaoUtils=new GreenDaoUtils();
                }
            }
        }

        return greenDaoUtils;
    }
    //初始化数据   这个方法在Application里面调用
    public void init(){
        setDataBase();
    }

    private void setDataBase() {
        //调用Application里面的上下文   参数二为数据库名字
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(Mapp.context, "user.db", null);

        database = helper.getWritableDatabase();

        DaoMaster daoMaster= new DaoMaster(database);

        daoSession = daoMaster.newSession();
    }

    public DaoSession getDaoSession(){

        return daoSession;
    }

    public SQLiteDatabase getSQLiteDatabase(){
        return database;
    }
}
 
  

初始化:


public class Mapp extends Application {

    public static Context context;
    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        //初始化
        GreenDaoUtils.getmInstance().init();
    }
}



Maintivity:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Button sousuo;
    private Button shan;
    private EditText content;
    private ListView lv;
    List list;
    private String s;
    private Myadpater myadpater;
    private UsersDao usersDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        sousuo = (Button) findViewById(R.id.sousuo);
        shan = (Button) findViewById(R.id.delete);
        content=findViewById(R.id.guanjian);


        //greendao工具类
        DaoSession daoSession = GreenDaoUtils.getmInstance().getDaoSession();
        usersDao = daoSession.getUsersDao();
        list= usersDao.loadAll();//加载所有数据

        sousuo.setOnClickListener(this);
        shan.setOnClickListener(this);


        lv = (ListView) findViewById(R.id.lv);
        //是哦配齐
        myadpater = new Myadpater();
        lv.setAdapter(myadpater);

        //点击事件
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                startActivity(new Intent(MainActivity.this, Main2Activity.class));
            }
        });


    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sousuo:
                //获取输入的内容
                s = content.getText().toString().trim();
                tianJia();
                list=usersDao.loadAll();//查看所有
                myadpater.notifyDataSetChanged();

                break;
            case R.id.delete:

                usersDao.deleteAll();//删除所有
                list=usersDao.loadAll();//查看所有
                myadpater.notifyDataSetChanged();

                break;

        }

    }

    //添加
    private void tianJia() {
        Users users = new Users(s);
        usersDao.insert(users);
    }

    class Myadpater extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = View.inflate(MainActivity.this, R.layout.listview_item, null);

            }
            TextView textView = (TextView) view.findViewById(R.id.lv_item);
            textView.setText(list.get(i).getName());
            return view;
        }
    }
}






你可能感兴趣的:(Andriod开发)