private void setSearchView() {
mSearchView=(SearchView) findViewById(R.id.search);
mSearchView.setOnQueryTextListener(this);//设定监听器
//mSearchView.setIconifiedByDefault(true);//设置另外一种样式,默认为true
mSearchView.onActionViewExpanded();//设置默认展开的searchView
//mSearchView.setSubmitButtonEnabled(true);//是否显示提交按钮 效果不好,显示
//借助反射修改searchView中某个对象的背景
try {
//获得文本编辑框
Field f=mSearchView.getClass().getDeclaredField("mQueryTextView");
f.setAccessible(true);
AutoCompleteTextView at=(AutoCompleteTextView) f.get(mSearchView);
at.setTextColor(Color.RED);//修改字体
//at.setFocusable(false);
//利用反射获得背景对应的线性布局
// Field f1=mSearchView.getClass().getDeclaredField("mSearchPlate");
// f1.setAccessible(true);
// LinearLayout lv=(LinearLayout) f1.get(mSearchView);
// lv.setBackgroundResource(R.drawable.edittext_shape_1);
//法2:改变对应背景
//根据view id获得一个资源的在R文件中的id,返回对应id值,不存在返回0.
int ids=getResources().getIdentifier("android:id/search_plate", null,null);
Log.i("TAG",ids+"--666");
LinearLayout lv=(LinearLayout) mSearchView.findViewById(ids);
lv.setBackgroundResource(R.drawable.edittext_selector_1);
int id_btn=getResources().getIdentifier("android:id/search_button", null,null);
ImageView sb=(ImageView) mSearchView.findViewById(id_btn);
sb.setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
}
布局文件:
<SearchView
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="请输入搜索内容"
android:imeOptions="actionSearch"
></SearchView>
2.listview SimpleCursorAdapter
private void setListView() {
lv=(ListView) findViewById(R.id.listview);
adapter=new SimpleCursorAdapter(this,
R.layout.list_item,
doQuery(),
new String[]{"content","created"},
new int[]{R.id.content,R.id.created},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
lv.setAdapter(adapter);
//注册上下文菜单
registerForContextMenu(lv);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0,201,1,"删除");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info=(AdapterContextMenuInfo) item.getMenuInfo();
final long id=info.id;//此值对应记录的主键值
//执行删除操作 根据id删除指定记录
//根据id删除执行删除动作,删除完之后更新listview
long rows=dao.delete("notetab","_id=?",new String[]{id+""});//String.valueOf(id);
//更新listview 交换cursor对象,重新查询
cursor=dao.query(sql,null);
adapter.changeCursor(cursor);
//adapter.notifyDataSetChanged();//刷新数据
if(rows!=-1){
Toast.makeText(NotepadListActivity.this, "删除成功!", Toast.LENGTH_SHORT)
.show();
}
return super.onContextItemSelected(item);
}
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
String str="select * from notetab where content like ?";
Cursor c=dao.query(str, new String[]{"%"+newText+"%"});
adapter.changeCursor(c);
Log.i("TAG", "666"+c);
return false;
}
@Override
protected void onResume() {
cursor=dao.query(sql,null);
//交换cursor对象
adapter.changeCursor(cursor);
adapter.notifyDataSetChanged();//刷新数据
super.onResume();
}
@Override
protected void onDestroy() {
if(cursor!=null)cursor.close();//释放资源
super.onDestroy();
}
创建数据库语句:String sql="create table notetab("+
"_id integer primary key autoincrement,"+