액션바에 아래와 같이 검색창을 넣을 수 있는데 이를 위해서는 3가지 작업이 필요하다.
1) menu xml에 SearchView item추가
<item android:id="@+id⁄action_search" android:showAsAction="ifRoom" android:actionViewClass="android.widget.SearchView" android:title="@string⁄search" android:icon="@drawable⁄search" ><⁄item>
2) onCreateOptionsMenu 에 search view 항목추가 및 Listener 설정
@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.activity_lost, menu); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { searchItem = menu.findItem(R.id.action_search); searchView = (SearchView) searchItem.getActionView(); searchView.setQueryHint("물품명 또는 분류"); searchView.setOnQueryTextListener(queryTextListener); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); if(null!=searchManager ) { searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); } searchView.setIconifiedByDefault(true); } return true; }
개발참고사이트 : http://developer.android.com/reference/android/widget/SearchView.html
setQueryHint(CharSequence) |
쿼리 필드가 비어있을 때 나타나는 문장 설정 |
setOnQueryTextListener(SearchView.OnQueryTextListener listener) |
쿼리 변경시 사용자 액션의 listener 설정 |
setSearchableInfo(SearchableInfo searchable) | SearchableInfo 설정 |
setIconifiedByDefault(boolean iconified) | 검색창의 기본상태 설정 true : 필드가 보임 false : 아이콘으로 보임 |
3) 리스너에 처리로직 구현
private OnQueryTextListener queryTextListener = new OnQueryTextListener() { @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override public boolean onQueryTextSubmit(String query) { ⁄⁄ TODO Auto-generated method stub ⁄⁄searchItem.collapseActionView(); ⁄⁄Log.d(TAG,"onQueryTextSubmit"); Cursor mCount= db.rawQuery("select count(*) from datainfo where GetName like '%" + query+"%' or cate like '%"+ query+"%'", null); mCount.moveToFirst(); count= mCount.getInt(0); mCount.close(); if(count <=0){ Toast.makeText(LostActivity.con, "검색된 데이타가 없습니다.", Toast.LENGTH_LONG).show(); return false; } datainfo = new DataInfo[count]; Cursor result = db.rawQuery("SELECT * from datainfo where GetName like '%" + query+"%' or cate like '%"+ query+"%' order by GetDate desc", null); result.moveToFirst(); int i =0; while (i<count){ datainfo[i] = new DataInfo(); datainfo[i].SetLostId(result.getString(1)); datainfo[i].SetLostName(result.getString(2)); datainfo[i].SetLostURL(result.getString(3)); datainfo[i].SetLostDate(result.getString(5)); datainfo[i].SetLostTitle(result.getString(4)); datainfo[i].SetTakePlace(result.getString(6)); datainfo[i].SetTakeContact(result.getString(7)); datainfo[i].SetLostPos(result.getString(9)); datainfo[i].SetLostPlace(result.getString(10)); datainfo[i].SetLostThing(result.getString(11)); datainfo[i].SetLostStatus(result.getString(12)); datainfo[i].SetLostImageUrl(result.getString(14)); i++; result.moveToNext(); } result.close(); appendRow(datainfo); resizeRow(count); ⁄⁄Log.d(TAG,"onQueryTextSubmit"+count); imm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0); searchView.setQuery("", false); searchView.setIconified(true); ⁄⁄Toast.makeText(LostActivity.con, "onQueryTextSubmit:["+count+"]", Toast.LENGTH_LONG).show(); return false; } @Override public boolean onQueryTextChange(String newText) { // TODO Auto-generated method stub return false; } };
'프로그래밍 > 안드로이드' 카테고리의 다른 글
[안드로이드]프로그먼트(fragment) (0) | 2013.04.11 |
---|---|
[안드로이드]액션바에 tab과 swipe기능 넣기-소스포함- (0) | 2013.04.10 |
[안드로이드] 액션 항목(action item) - 버튼 추가 소스포함- (0) | 2013.04.09 |
[안드로이드]액션바에 대하여.. (1) | 2013.04.09 |
서울시 공공API사용방법 (0) | 2013.04.03 |