Android实现EditText查询数据库内容

我们在app开发的时候需要使用数据库,那么如何使用EditText查询数据库内容呢?

首先我们要先添加一个布局,代码如下

其中添加了一个EditText,和一个ListView实现监听

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#c0dae1"
android:orientation="vertical"
android:id="@+id/sear_ac">

<LinearLayout
android:id="@+id/title_ss"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:background="#c0dae1"
android:gravity="center"
>

<!.......添加EditText......>

<EditText
android:id="@+id/soushuoEd_ss"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1"
android:background="@drawable/background_ellipse"/>

</LinearLayout>

<!.......添加ListView......>
<ListView
android:id="@+id/list_ss"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title_ss"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:fadingEdge="none"
android:fastScrollEnabled="true"
android:divider="#00000000"
android:dividerHeight="20dp"
></ListView>


</RelativeLayout>

2.ListView需要一个布局用于数据的装载,我把他命名为list_item,布局代码如下:

其中添加了两个TextView用于承载从数据查询的数据

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c0dae1"
android:orientation="horizontal" >

 

<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#FFC0CB">

<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginRight="15dp"
android:gravity="left"
android:textColor="#000000"
android:textSize="15dp" />

<TextView
android:id="@+id/tv2"
android:layout_gravity="left"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textColor="#000000"
android:textSize="20dp" />
</LinearLayout>

</RelativeLayout>

3.有了布局,现在就开始实现功能

首先搭建数据库帮助类(方法自行百度)

然后在需要搜索的页面添加代码

//初始化EditText编辑框

EditText SearEd=(EditText)findViewById(R.id.soushuoEd_ss);

//调用EditText框的事件监听

SearEd.addTextChangedListener(SearEdwatcher);

//实现Searwatcher方法

private TextWatcher SearEdwatcher = new TextWatcher(){

public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
Refresh();

//我在这儿使用了一个Refresh()方法,通过TextWatcher不停的触发该方法
}

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub

}

};

4.Refresh()方法,用于从数据库查询数据

private void Refresh() {
// TODO Auto-generated method stub
//初始化ListView
ListView liv=(ListView)findViewById(R.id.list_ss);
//获取搜索框的文本并除去空格后用于cur索引
String Ind=((EditText)findViewById(R.id.soushuoEd_ss)).getText().toString();
String Ind2=Ind.replace(" ", "");

//对ind字符串进行除去空格操作
Log.v("搜索框的字符串", Ind2);
try{
//获取数据游标,模糊查询与输入值相符的数据以id降序排列
Cursor cu=db.query("select * from note_table where (times_values like '%"+Ind2+ "%') or " +
"(times like '%"+Ind2+"%') order by _id DESC",null);

if(cu.moveToFirst()==true){

//绑定数据操作
SimpleCursorAdapter sim=new SimpleCursorAdapter(SearchActivity.this,R.layout.list_item1, cu,
new String[]{"db.str1","db.str2"}, new int[]{R.id.tv1,R.id.tv2});

liv.setAdapter(sim);
}else{
Log.v("数据查询","continue");
}
}catch(Exception e){
Log.v("查询失败","打印log日志");
}
};

 

 

 

posted on 2016-05-02 16:36  似曾相识丶xjj  阅读(1006)  评论(0编辑  收藏  举报