Skip to content
This repository has been archived by the owner on Apr 30, 2023. It is now read-only.

Commit

Permalink
对搜索功能进行了更改
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonNOV committed Apr 1, 2021
1 parent f24ca6a commit c278010
Show file tree
Hide file tree
Showing 26 changed files with 729 additions and 71 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

## 📘开发日志

### 2021/04/01
- 对搜索功能进行了更改
- **搜索功能部分代码还未进行清理**

### 2021/03/31
- 各搜索结果页面的适配器均已加入,各页面的部分代码还需要进行完善
- 搜索结果页面已基本完善
Expand Down
19 changes: 19 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ android {
}
}


// GreenDaoPlugins
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'

greendao {
// 数据库版本号
schemaVersion 1

// 生成数据库文件的目录
targetGenDir 'src/main/java'

// 生成的数据库相关文件的包名
daoPackage 'com.leon.biuvideo.greendao.dao'
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
Expand Down Expand Up @@ -100,5 +116,8 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

// GreenDao
implementation 'org.greenrobot:greendao:3.3.0'

implementation 'androidx.palette:palette:1.0.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,50 @@

import android.content.Context;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.leon.biuvideo.R;
import com.leon.biuvideo.adapters.baseAdapters.BaseAdapter;
import com.leon.biuvideo.adapters.baseAdapters.BaseViewHolder;
import com.leon.biuvideo.greendao.dao.SearchHistory;

import java.util.List;

public class SearchHistoryAdapter extends BaseAdapter<String> {
private final List<String> historys;
private final Context context;
/**
* @Author Leon
* @Time 2021/3/1
* @Desc 搜索历史数据适配器
*/
public class SearchHistoryAdapter extends BaseAdapter<SearchHistory> {
private final List<SearchHistory> searchHistoryList;

public SearchHistoryAdapter(List<String> historys, Context context) {
super(historys, context);
private OnSearchHistoryListener onSearchHistoryListener;

this.historys = historys;
this.context = context;
public SearchHistoryAdapter(List<SearchHistory> beans, Context context) {
super(beans, context);

this.searchHistoryList = beans;
}

public interface OnSearchHistoryListener {
/**
* 删除搜索历史
*
* @param searchHistory 被删除的搜索历史
*/
void onDelete(SearchHistory searchHistory);

/**
* 点击搜索历史
*
* @param keyword 关键字
*/
void onClick(String keyword);
}

public void setOnSearchHistoryListener(OnSearchHistoryListener onSearchHistoryListener) {
this.onSearchHistoryListener = onSearchHistoryListener;
}

@Override
Expand All @@ -30,18 +55,24 @@ public int getLayout(int viewType) {

@Override
public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
SearchHistory searchHistory = searchHistoryList.get(position);

holder
.setText(R.id.history_item_keyword, historys.get(position))
.setText(R.id.history_item_keyword, searchHistory.getKeyword())
.setOnClickListener(R.id.history_item_delete, new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "删除-" + historys.get(position), Toast.LENGTH_SHORT).show();
if (onSearchHistoryListener != null) {
onSearchHistoryListener.onDelete(searchHistoryList.get(position));
}
}
})
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Keyword:" + historys.get(position), Toast.LENGTH_SHORT).show();
if (onSearchHistoryListener != null) {
onSearchHistoryListener.onClick(searchHistory.getKeyword());
}
}
});

Expand Down
144 changes: 144 additions & 0 deletions app/src/main/java/com/leon/biuvideo/greendao/dao/DaoBaseUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.leon.biuvideo.greendao.dao;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.List;

/**
* @Author Leon
* @Time 2021/4/1
* @Desc CRUD基础类
*/
public class DaoBaseUtils<T> {
private final DaoSession daoSession;
private final Class<T> entityClass;
private final AbstractDao<T, Long> entityDao;

public DaoBaseUtils(Class<T> entityClass, AbstractDao<T, Long> entityDao, DaoSession daoSession) {
this.daoSession = daoSession;
this.entityClass = entityClass;
this.entityDao = entityDao;
}

/**
* 插入单条数据
*
* @param entity 数据
* @return 插入状态
*/
public boolean insert(T entity) {
return entityDao.insert(entity) != -1;
}

/**
* 插入多条数据
*
* @param entityList 数据集合
* @return 插入状态
*/
public boolean insertMultiple (final List<T> entityList) {
try {
daoSession.runInTx(new Runnable() {
@Override
public void run() {
for (T entity : entityList) {
daoSession.insertOrReplace(entity);
}
}
});

return true;
} catch (Exception e) {
return false;
}
}

/**
* 修改数据
*
* @param entity 新数据
* @return 修改状态
*/
public boolean update (T entity) {
try {
daoSession.update(entity);
} catch (Exception e) {
e.printStackTrace();
}

return false;
}

/**
* 删除数据
*
* @param entity 被删除数据
* @return 删除状态
*/
public boolean delete (T entity) {
try {
daoSession.delete(entity);
return true;
} catch (Exception e) {
e.printStackTrace();
}

return false;
}

/**
* 删除所有数据
*
* @return 删除状态
*/
public boolean deleteAll () {
try {
daoSession.deleteAll(entityClass);
return true;
} catch (Exception e) {
e.printStackTrace();
}

return false;
}

/**
* 查询所有数据
*
* @return 查询到的结果
*/
public List<T> queryAll () {
return daoSession.loadAll(entityClass);
}

/**
* 根据ID进行查询
*
* @param key ID/Key
* @return 查询到的数据
*/
public T queryById (long key) {
return daoSession.load(entityClass, key);
}

/**
* 使用native sql进行查询
*
* @param sql SQL语句
* @param conditions 条件
* @return 查询到的结果
*/
public List<T> queryByNativeSql (String sql, String[] conditions) {
return daoSession.queryRaw(entityClass, sql, conditions);
}

/**
* 使用queryBuilder进行查询
*/
public List<T> queryByQueryBuilder (WhereCondition condition, WhereCondition ... conditionsMore) {
QueryBuilder<T> queryBuilder = daoSession.queryBuilder(entityClass);
return queryBuilder.where(condition, conditionsMore).list();
}
}
71 changes: 71 additions & 0 deletions app/src/main/java/com/leon/biuvideo/greendao/dao/DaoManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.leon.biuvideo.greendao.dao;

import android.content.Context;

/**
* @Author Leon
* @Time 2021/4/1
* @Desc Dao管理器
*/
public class DaoManager {
private static final String DB_NAME = "biuvideo";
private DaoMaster daoMaster;
private DaoSession daoSession;
private final Context context;
private DaoMaster.DevOpenHelper devOpenHelper;

public DaoManager(Context context) {
this.context = context;
}

/**
* 判断是否存在数据库,如果没有就创建
*/
public DaoMaster getDaoMaster () {
if (daoMaster == null) {
devOpenHelper = new DaoMaster.DevOpenHelper(context, DB_NAME);
daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
}

return daoMaster;
}

/**
* 获取Session对象
*
* @return DaoSession
*/
public DaoSession getDaoSession () {
if (daoSession == null) {
if (daoMaster == null) {
daoMaster = getDaoMaster();
}

daoSession = daoMaster.newSession();
}

return daoSession;
}

/**
* 关闭与数据库的链接
*/
public void closeConnection () {
closeHelper();
closeDaoSession();
}

private void closeHelper () {
if (devOpenHelper != null) {
devOpenHelper.close();
devOpenHelper = null;
}
}

private void closeDaoSession () {
if (daoSession != null) {
daoSession.clear();
daoSession = null;
}
}
}
Loading

0 comments on commit c278010

Please sign in to comment.