Searchview的运用和数据库的相结合使用

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

public class MyOpenHelper extends SQLiteOpenHelper {
    //把数据的名字写死
    public static final String db_name="history.db";
    //把表的名字写死
    public static final String table_name="history";
    //id integer primary key autoincrement 序号自增长
    public static final String create_table_sql="create table "+table_name+"( id integer primary key autoincrement, history text)";
    public MyOpenHelper(@Nullable Context context) {
        super(context, db_name, null, 1);
    }

   /* public MyOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }*/

    @Override
    public void onCreate(SQLiteDatabase db) {
        //数据库表的创建
        db.execSQL(create_table_sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    //插入数据
    public void inserthistory(String history){
        //首先得到一个可写的数据库
        SQLiteDatabase db=getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        //插入数据
        contentValues.put("history",history);
        db.insert(table_name,null,contentValues);
    }
    //删除指定数据
    /*public void deletehistory(String name){
        //首先得到一个可写的数据库
        SQLiteDatabase db=getWritableDatabase();
        db.delete(table_name,"name = ?",new String[] {name});
    }*/
    public void deletehistory(){
        //首先得到一个可写的数据库
        SQLiteDatabase db=getWritableDatabase();
        db.delete(table_name,null,null);}


        /*更新数据
        public void updatehistory(Student student){
        //首先得到一个可写的数据库
        SQLiteDatabase db=getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        //插入数据
        contentValues.put("name",student.getName());
            contentValues.put("gender",student.getGender());
            //意思是把这个名字的学生的数据换了
        db.update(table_name,contentValues,"name = ?",new String[] {student.getName()});
    }*/
    //查询数据
    public List<String> queryhistory() {
        List<String> historylist = new ArrayList<>();
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = db.query(table_name, null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                // 遍历Cursor对象,取出数据并打印
                String history = cursor.getString(cursor.getColumnIndex("history"));
                historylist.add(history);
            } while (cursor.moveToNext());
        }
        cursor.close();
    return historylist;
    }

}







import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

public class Searchviewtest extends AppCompatActivity implements View.OnClickListener {
    private ListView mListView;
    androidx.appcompat.widget.SearchView mSearchView;
    List<String> stringList=new ArrayList<>();
    Adapter_searchview adapter_searchview;
    MyOpenHelper myOpenHelper;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchviewtest);
        button=findViewById(R.id.button);
        mSearchView = findViewById(R.id.searchView);
        mListView = (ListView) findViewById(R.id.listView);
        myOpenHelper=new MyOpenHelper(this);
        //得到历史数据
        gethistory();
        button.setOnClickListener(this);
        mListView.setTextFilterEnabled(true);
        // 设置搜索文本监听
        mSearchView.setOnQueryTextListener(new androidx.appcompat.widget.SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //插入搜素的历史数据
                myOpenHelper.inserthistory(query);
                gethistory();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (!TextUtils.isEmpty(newText)){
                    mListView.setFilterText(newText);
                }else{
                    mListView.clearTextFilter();
                }
                return false;
            }
        });


    }

    private void gethistory() {
        //查询并展示历史数据
        stringList=myOpenHelper.queryhistory();
        //清除历史数据的按钮按照数据的有无决定展示或者不展示
        if (stringList.size()==0){
            button.setVisibility(View.GONE);
        }
        else button.setVisibility(View.VISIBLE);
        adapter_searchview=new Adapter_searchview(this,stringList);
        mListView.setAdapter(adapter_searchview);
    }


    @Override
    public void onClick(View v) {
        myOpenHelper.deletehistory();
        gethistory();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值