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

本文介绍如何在Android应用中利用GreenDao库来实现搜索历史的存储和删除功能。首先,需要配置项目,确保GreenDao库的正确引入。接着,创建对应的bean类并生成相关代码。然后,通过工具类进行数据库操作,包括插入搜索历史记录和查询历史记录。最后,在MainActivity中实现展示和删除搜索历史的用户交互。

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<Users> 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;
        }
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值