greendao和流式布局的查询和清空搜索历史

先是权限
classpath ‘org.greenrobot:greendao-gradle-plugin:3.2.2’
apply plugin: ‘org.greenrobot.greendao’
//GreenDao数据库
implementation ‘org.greenrobot:greendao:3.2.2’
//Butterknife:根据反射注入框架
implementation ‘com.jakewharton:butterknife:8.8.1’
annotationProcessor ‘com.jakewharton:butterknife-compiler:8.8.1’
implementation ‘com.fynn.fluidlayout:fluidlayout:1.0’

activity_main布局:








**myheander**

**app注册和建表** public class App extends Application { private static DaoSession daoSession;
@Override
public void onCreate() {
    super.onCreate();

    DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, "zxf", null);
    SQLiteDatabase database = devOpenHelper.getWritableDatabase();
    DaoMaster daoMaster = new DaoMaster(database);
    daoSession = daoMaster.newSession();
}

public static DaoSession getDaoSession() {
    return daoSession;
}

}
TitleItem
public class TitleItem extends LinearLayout {
//2.定义接口对象
private OnTitleClickListener listener;
private EditText titleEd;
private Button titleBut;

public TitleItem(Context context) {
    this(context,null);
}

public TitleItem(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs,0);
}

public TitleItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    //初始化控件上的方法
    initView(context);
}

/**
 * 初始化控件上的方法
 * @param context
 */
private void initView(Context context) {
    View view = View.inflate(context, R.layout.my_heander, this);
    titleEd=view.findViewById(R.id.title_ed);
    titleBut=view.findViewById(R.id.title_but);
    titleBut.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String s = titleEd.getText().toString();
            //5.添加一个触发事件
            listener.onClick(s);
        }
    });
}
//1.定义一个接口
public interface OnTitleClickListener{
    void onClick(String s);
}
//3为接口对象赋值的方法
void setOnTitleClickListener(OnTitleClickListener listener){
    this.listener=listener;
}

}

**MainActivity **
public class MainActivity extends AppCompatActivity {

@BindView(R.id.main_title)
TitleItem mainTitle;
@BindView(R.id.fluid_layout)
FluidLayout fluidLayout;
@BindView(R.id.but_clear)
Button butClear;
private List list;
private UserDao historicalBeanDao;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    list = new ArrayList<>();
    //创建数据库
    historicalBeanDao=App.getDaoSession().getUserDao();
    //得到自定义控件上的值
    mainTitle.setOnTitleClickListener(new TitleItem.OnTitleClickListener() {
        @Override
        public void onClick(String s) {
            //当前页面中的控件清空
            fluidLayout.removeAllViews();
            //创建自定义的HistoricalBean
            User historicalBean = new User();
            //把从自定义控件上得到的s值放到bean对象中
            historicalBean.setName(s);
            //把对象存到数据库中,不添加重复的字段名
            MainActivity.this.historicalBeanDao.insertOrReplace(historicalBean);
            //清空list中的数据
            list.clear();
            //添加完数据后,再次查询数据库,把刚才添加的搜索内容,展示出来
            list.addAll(MainActivity.this.historicalBeanDao.loadAll());
            genTag();
        }
    });
    //查询数据库
    list.addAll(this.historicalBeanDao.loadAll());
    genTag();

}

private void genTag() {
    for (int x = 0; x < list.size(); x++) {
        tv = new TextView(MainActivity.this);
        tv.setText(list.get(x).getName());
        tv.setTextSize(16);
        FluidLayout.LayoutParams params = new FluidLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(12, 12, 12, 12);
        fluidLayout.addView(tv, params);
    }
}

@OnClick(R.id.but_clear)
public void onClick() {
    fluidLayout.removeAllViews();
    historicalBeanDao.deleteAll();
}

}

你可能感兴趣的:(greendao和流式布局的查询和清空搜索历史)