android - 搜索列表视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3550024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android - search listview?
提问by James Testa
Is it possible with Android to have a search bar for a ListView so that when the search bar is touched a keyboard pops up, and when text is typed into the search bar, the items that match in the ListView are shown?
Android 是否可以为 ListView 设置一个搜索栏,以便在触摸搜索栏时弹出键盘,并且当在搜索栏中输入文本时,会显示 ListView 中匹配的项目?
What I really need is the search bar that brings up a keyboard.
我真正需要的是带键盘的搜索栏。
Update:
更新:
I've added the EditText field that brings up a keyboard and I can type into the EditText field. What I want is to have the first few characters of the items in the list shown in the ListView match the characters typed into the EditText window.
我已经添加了弹出键盘的 EditText 字段,我可以在 EditText 字段中输入。我想要的是让 ListView 中显示的列表中项目的前几个字符与输入到 EditText 窗口中的字符匹配。
I've tried following the approach listed here ListView Filterbut I am a little confused as to how much filtering is already done in ListView?
我已经尝试按照此处列出的方法ListView Filter但我对 ListView 中已经完成了多少过滤有点困惑?
1) Do I need to create a separate array that stores the values that match the text typed into EditText? From this post Call adapter.notifyDataSetChanged, it appears that ListView already has a shadow array to do this, and it gets updated when adapter.notifyDataSetChanged(); is called.
1) 我是否需要创建一个单独的数组来存储与输入到 EditText 中的文本匹配的值?从这篇文章Call adapter.notifyDataSetChanged来看,ListView 似乎已经有一个阴影数组来执行此操作,并且它会在 adapter.notifyDataSetChanged(); 时更新;叫做。
2) Do I need to call adapter.notifyDataSetChanged(); to have ListView updated after I type some text in the EditText window?
2) 是否需要调用adapter.notifyDataSetChanged(); 在 EditText 窗口中键入一些文本后更新 ListView 吗?
3) Do I need to extend ListActivity as this postindicates? If so how do I extend my activity class if the activity class is already being extended from the main activity?
3)我是否需要像这篇文章所说的那样扩展 ListActivity ?如果是这样,如果活动类已经从主活动扩展,我该如何扩展我的活动类?
4) What I currently have is the following:
4)我目前拥有的是以下内容:
ArrayAdapter<String> adapter = null;
private EditText filterText = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.symptom);
ListView symptomList = (ListView) findViewById(R.id.ListView_Symptom);
symptomList.setTextFilterEnabled(true);
symptomList.setFastScrollEnabled(true);
filterText = (EditText) findViewById(R.id.search_box);
filterText.addTextChangedListener(filterTextWatcher);
adapter = new ArrayAdapter<String>(this, R.layout.menu_item, symptomsArray);
symptomList.setAdapter(adapter);
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
}
};
Unfortunately at the moment when I type in the EditText box, I get a NullPointer Exception in
不幸的是,当我在 EditText 框中键入时,我收到了 NullPointer Exception
Thread [<7> Filter] (Suspended (exception NullPointerException))
ArrayAdapter$ArrayFilter.performFiltering(CharSequence) line: 437
Filter$RequestHandler.handleMessage(Message) line: 234
Filter$RequestHandler(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
HandlerThread.run() line: 60
Any idea what I am missing?
知道我缺少什么吗?
采纳答案by Hardik Muliya
You have done very small mistake :- create array adapter before setting text changed Listener to the edit text
你犯了一个很小的错误:-在将文本更改监听器设置为编辑文本之前创建数组适配器
see the corrected code
查看更正后的代码
public class SearchListView extends Activity
{
/** Called when the activity is first created. */
private ListView lv1;
private String lv_arr[] =
{ "Android", "iPhone", "BlackBerry", "me", "J2ME", "Listview", "ArrayAdapter", "ListItem", "Us", "UK", "India" };
ListView lst;
EditText edt;
ArrayAdapter<String> arrad;
@Override
public void onCreate( Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
edt = (EditText) findViewById(R.id.EditText01);
arrad = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr);
lv1.setAdapter(arrad);
// By using setTextFilterEnabled method in listview we can filter the listview items.
lv1.setTextFilterEnabled(true);
edt.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged( CharSequence arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged( CharSequence arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged( Editable arg0)
{
// TODO Auto-generated method stub
SearchListView.this.arrad.getFilter().filter(arg0);
}
});
}
}
}
回答by noah
If you implement Filterablein your adapter, ListView can handle the filtering.
如果您在适配器中实现Filterable,则ListView 可以处理过滤。
回答by gregS
As yet another approach, the ListView itself can show the text that the user is entering (as opposed to using a separate TextView) by handling the onKeyUp events, scrolling the ListView to the item that has the entered text, and underlining that text in the ListView. I use AsyncTask to implement the ListView text search and that results in a convenient type ahead capability.
作为另一种方法,ListView 本身可以通过处理 onKeyUp 事件,将 ListView 滚动到具有输入文本的项目,并在列表显示。我使用 AsyncTask 来实现 ListView 文本搜索,这导致了一种方便的提前输入功能。
回答by Sushil
This page has working example of what you are looking for http://learningsolo.com/android-text-search-filter-with-textwatcher-in-listview-example/
这个页面有你正在寻找的工作示例 http://learningsolo.com/android-text-search-filter-with-textwatcher-in-listview-example/
Add search box in app bar layout:
在应用栏布局中添加搜索框:
<android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay">
<!-- To add Search Text box to action bar -->
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay">
<EditText android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/searchProjectTxt" android:layout_marginLeft="30dp" android:singleLine="true" android:hint="Search Here..." />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Then add TextWatcher on "searchProjectTxt" to add filter
然后在“searchProjectTxt”上添加 TextWatcher 以添加过滤器
EditText searchTxt = (EditText)findViewById(R.id.searchProjectTxt);
searchTxt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
filter(cs.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
}
void filter(String text){
List<ProjectsVo> temp = new ArrayList();
for(ProjectsVo d: projectListCache){
if(d.getTitle().toLowerCase().contains(text.toLowerCase())){
temp.add(d);
}
}
projectList.clear();
projectList.addAll(temp);
adapter.notifyDataSetChanged();
}