我想在listview中使用搜索框实现部分单词搜索的过滤功能。我已经建立了以下搜索机制,如何获得部分单词搜索功能?
EG。如果我搜索“stackover ..”,则会出现stackoverflow,但是如果我搜索“tackover ..”stackoverflow没有出现,我需要它来搜索单词中的部分匹配。
这是我的代码
et = (EditText) findViewById(R.id.EditText01);
et.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
textlength = et.getText().length();
array_sort.clear();
for (int i = 0; i < actorsList.size(); i++)
{
String nama = actorsList.get(i).getName();
if (textlength <= nama.length())
{
if (et.getText().toString().equalsIgnoreCase((String) nama.subSequence(0, textlength)) )
{
array_sort.add(actorsList.get(i));
}
}
}
if (array_sort.isEmpty())
{
textView.setText(R.string.empty);
}
else
{
textView.setText("");
}
adapter = new ActorAdapter(getApplicationContext(), R.layout.row, array_sort);
listview.setAdapter(adapter);
}
});
任何帮助将不胜感激。感谢
答案 0 :(得分:0)
filterEditText = (EditText)findViewById(R.id.filter);
filterEditText.addTextChangedListener(filterTextWatcher);
TextWatcher filterTextWatcher = new TextWatcher() {
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);
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
适配器必须实现Filterable
@Override
public Filter getFilter() {
if(filter == null)
filter = new CheeseFilter();
return filter;
}
过滤类:
public class CheeseFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
// TODO Auto-generated method stub
constraint = constraint.toString().toLowerCase();
FilterResults newFilterResults = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<String> auxData = new ArrayList<String>();
for (int i = 0; i < data.size(); i++) {
if (data.get(i).toLowerCase().contains(constraint))
auxData.add(data.get(i));
}
newFilterResults.count = auxData.size();
newFilterResults.values = auxData;
} else {
newFilterResults.count = data.size();
newFilterResults.values = data;
}
return newFilterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<String> resultData = new ArrayList<String>();
resultData = (ArrayList<String>) results.values;
EfficientAdapter adapter = new EfficientAdapter(context, resultData);
list.setAdapter(adapter);
}
}
答案 1 :(得分:0)
此页面提供了您正在寻找的工作示例 http://learningsolo.com/android-text-search-filter-with-textwatcher-in-listview-example/
在应用栏布局中添加搜索框:
<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();
}