android-搜索列表视图?
Android是否可以为ListView设置一个搜索栏,以便在触摸搜索栏时弹出键盘,在搜索栏中键入文本时,显示与ListView匹配的项目 我真正需要的是打开键盘的搜索栏 更新: 我已经添加了EditText字段,可以打开键盘,我可以在EditText字段中键入内容。我想要的是让ListView中显示的列表中项目的前几个字符与EditText窗口中键入的字符匹配 我尝试了下面列出的方法,但对于ListView中已经完成了多少过滤,我有点困惑 1) 我是否需要创建一个单独的数组来存储与输入到EditText中的文本匹配的值?在本文中,ListView似乎已经有了一个影子数组来执行此操作,并且在adapter.notifyDataSetChanged()时它会得到更新;被称为 2) 我需要调用adapter.notifyDataSetChanged()吗;在EditText窗口中键入一些文本后更新ListView 3) 我是否需要按此指示扩展ListActivity?如果是这样,如果活动类已经从主活动扩展而来,我如何扩展活动类 4) 我目前的情况如下:android-搜索列表视图?,android,Android,Android是否可以为ListView设置一个搜索栏,以便在触摸搜索栏时弹出键盘,在搜索栏中键入文本时,显示与ListView匹配的项目 我真正需要的是打开键盘的搜索栏 更新: 我已经添加了EditText字段,可以打开键盘,我可以在EditText字段中键入内容。我想要的是让ListView中显示的列表中项目的前几个字符与EditText窗口中键入的字符匹配 我尝试了下面列出的方法,但对于ListView中已经完成了多少过滤,我有点困惑 1) 我是否需要创建一个单独的数组来存储与输入到Edi
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();
}
};
ArrayAdapter适配器=null;
private EditText filterText=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.symptom);
ListView症状列表=(ListView)findViewById(R.id.ListView\u症状);
症状列表。setTextFilterEnabled(true);
症状列表。setFastScrollEnabled(真);
filterText=(EditText)findViewById(R.id.search_框);
filterText.addTextChangedListener(filterTextWatcher);
适配器=新阵列适配器(此,R.layout.menu_项,症状阵列);
症状列表。设置适配器(适配器);
私有TextWatcher filterTextWatcher=新TextWatcher(){
公共无效后文本已更改(可编辑){
}
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
}
public void onTextChanged(字符序列,int start,int before,
整数计数){
adapter.getFilter().filter;
adapter.notifyDataSetChanged();
}
};
不幸的是,当我在EditText框中键入时,我在中得到了一个NullPointer异常
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
Thread[Filter](挂起(异常NullPointerException))
ArrayAdapter$ArrayFilter.PerformFilter(字符序列)行:437
筛选器$RequestHandler.handleMessage(消息)行:234
筛选器$RequestHandler(处理程序)。dispatchMessage(消息)行:99
Looper.loop()行:123
HandlerThread.run()行:60
知道我遗漏了什么吗?如果在适配器中实现,作为另一种方法,ListView本身可以显示用户输入的文本(而不是使用单独的TextView)通过处理onKeyUp事件,将ListView滚动到具有输入文本的项目,并在ListView中为该文本加下划线。我使用AsyncTask实现ListView文本搜索,从而实现方便的提前键入功能。您犯了一个很小的错误:-在设置文本更改侦听器之前创建数组适配器编辑文本 请参阅更正的代码
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);
}
});
}
公共类SearchListView扩展活动
{
/**在首次创建活动时调用*/
私有ListView lv1;
专用字符串lv_arr[]=
{“安卓”、“iPhone”、“黑莓”、“me”、“J2ME”、“Listview”、“ArrayAdapter”、“ListItem”、“美国”、“英国”、“印度”};
ListView lst;
编辑文本edt;
ArrayAdapter arrad;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
edt=(EditText)findViewById(R.id.EditText01);
arrad=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,lv\u arr);
lv1.设置适配器(arrad);
//通过在listview中使用setTextFilterEnabled方法,我们可以过滤listview项。
lv1.setTextFilterEnabled(true);
edt.addTextChangedListener(新的TextWatcher()
{
@凌驾
public void onTextChanged(字符序列arg0、int arg1、int arg2、int arg3)
{
//TODO自动生成的方法存根
}
@凌驾
更改前的公共void(字符序列arg0、int arg1、int arg2、int arg3)
{
//TODO自动生成的方法存根
}
@凌驾
public void PostTextChanged(可编辑arg0)
{
//TODO自动生成的方法存根
SearchListView.this.arrad.getFilter().filter(arg0);
}
});
}
}本页提供了您正在寻找的工作示例 在应用程序栏布局中添加搜索框:
<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>
然后在“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();
}
EditText searchTxt=(EditText)findviewbyd(R.id.searchProjectTxt);
searchTxt.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列cs、int arg1、int arg2、int arg3){
//当用户更改文本时
过滤器(cs.toString());
}
@凌驾
更改前的公共void(字符序列arg0、int arg1、int arg2、,
int arg3){
}
@凌驾
public void PostTextChanged(可编辑arg0){
}
});
}
无效过滤器(字符串文本){
List temp=new ArrayList();
for(ProjectsVo d:projectListCache){
if(d.getTitle().toLowerCase().contains(text.toLowerCase())){
温度添加(d);
}
}
projectList.clear();
projectList.addAll(tem