標籤:
我們在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日誌");
}
};
Android實現EditText查詢資料庫內容