首先进行布局 (需要借助前面博客里的代码)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main_bg"
android:orientation="vertical" >
<include layout="@layout/header" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="665"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3.2"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/search_bg"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="请输入搜索关键字"
android:textColor="#ffffff"
android:textSize="12sp" />
<EditText
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="@+id/search_text"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/search_text_bg"
android:inputType="text"
android:paddingLeft="5dp"
android:paddingRight="5dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/search_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/search_btn" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.1" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<include layout="@layout/footer" />
</LinearLayout>
完成监听
public class SearchActivity extends BaseActivity {
private Button searchBtn;
private EditText searchText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
super.init("搜索问题", 2);
searchBtn = (Button) findViewById(R.id.search_btn);
searchText = (EditText) findViewById(R.id.search_text);
searchBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String text = searchText.getText().toString();
Intent in = new Intent(SearchActivity.this,
QuestionListActivity.class);
in.putExtra("keyword", text);
startActivity(in);
}
});
}
}
修改分页查询方法,多加入一个关键字
public static List<Map<String, Object>> listDataPage(int pageNo,
int pageSize, String keyword) {
List<Map<String, Object>> allValues = new ArrayList<Map<String, Object>>();
String sql = "SELECT id,question,answer FROM question WHERE question LIKE ? LIMIT ?,?";
Cursor c = Globals.dbc.getReadableDatabase().rawQuery(
sql,
new String[] { "%" + keyword + "%",
(pageNo - 1) * pageSize + "", pageSize + "" });
c.moveToFirst();
while (!c.isAfterLast()) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", c.getInt(0));
map.put("question", c.getInt(0) + "、" + c.getString(1));
map.put("answer", c.getString(2));
allValues.add(map);
c.moveToNext();
}
c.close();
return allValues;
}
public static int getAllCount(String keyword) {
String sql = "SELECT COUNT(*) FROM question WHERE question LIKE ?";
Cursor c = Globals.dbc.getReadableDatabase().rawQuery(sql,
new String[] { "%" + keyword + "%" });
c.moveToFirst();
int count = c.getInt(0);
c.close();
return count;
}
修改ListActivity,处理关键字的接收和查询。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question_list);
// 接收关键字
keyword = getIntent().getStringExtra("keyword");
if (keyword == null) {
super.init("全部问题", 1);
keyword = "";
} else {
super.init("搜索: " + keyword, 1);
}
list = (ListView) findViewById(R.id.list);
allValues = QuestionDAOUtils.listDataPage(pageNo, pageSize,keyword);
// 查询记录数
count = QuestionDAOUtils.getAllCount(keyword);