package net.npaka.searchviewex;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
//検索ビュー
public class SearchViewEx extends Activity
implements OnQueryTextListener {
private ListView listView;
//アクティビティ生成時に呼ばれる
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
//レイアウトの生成
LinearLayout layout=new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);
//検索ビュー
SearchView searchView=new SearchView(this);
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
searchView.setSubmitButtonEnabled(true);
searchView.setQueryHint("検索文字を入力");
layout.addView(searchView);
//リストビュー
listView=new ListView(this);
listView.setScrollingCacheEnabled(false);
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1,
new String[]{"Cupcake","Donut","Eclair","Froyo",
"Gingerbread","Honeycomb","IceCreamSandwich"}));
listView.setTextFilterEnabled(true);
layout.addView(listView);
setContentView(layout);
}
//テキスト変更時に呼ばれる
@Override
public boolean onQueryTextChange(String query) {
if (TextUtils.isEmpty(query)) {
listView.clearTextFilter();
} else {
listView.setFilterText(query);
}
return true;
}
//サブミット時に呼ばれる
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
}
0 件のコメント:
コメントを投稿