我一直在尝试对listView进行简单的搜索,我已经能够使用Volley进行填充,但直到最后都无济于事。 它一直标记为无法解析方法' getFilter()
我也尝试了其他人的问题,这个问题与这个相同的搜索问题有关,但似乎没有一个对我有用,所以我别无选择,只能在这里发帖。 以下是我的代码
public class MainActivity extends Activity {
private ListView mList;
private List<Movie> movieList = new ArrayList<Movie>();
EditText inputSearch;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
mList = (ListView) findViewById(R.id.list);
inputSearch = (EditText) findViewById(R.id.inputSearch);
adapter = new CustomListAdapter(this, movieList);
mList.setAdapter(adapter);
fetchMovie();
// A method i declared in the program to load data from server with volley library which works fine
//SEARCH TEXTCHANGE
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
//FLAGS Cannot resolve method 'getFilter()' here
}
@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
}
});
}
}
这是CustomListAdapter代码,它填充了我的listView井
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
private String[] bgColors;
ImageLoader imageLoader = MyApplication.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Movie> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
bgColors = activity.getApplicationContext().getResources().getStringArray(R.array.movie_serial_bg);
}
@Override
public int getCount() {
return movieItems.size();
}
@Override
public Object getItem(int location) {
return movieItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row_image, null);
if (imageLoader == null)
imageLoader = MyApplication.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
TextView serial = (TextView) convertView.findViewById(R.id.serial);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView rating = (TextView) convertView.findViewById(R.id.rating);
TextView genre = (TextView) convertView.findViewById(R.id.genre);
TextView year = (TextView) convertView.findViewById(R.id.releaseYear);
// getting movie data for the row
Movie m = movieItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
// title
title.setText(m.getTitle());
// rating
rating.setText("Rating: " + String.valueOf(m.getRating()));
// genre
String genreStr = "";
for (String str : m.getGenre()) {
genreStr += str + ", ";
}
genreStr = genreStr.length() > 0 ? genreStr.substring(0,
genreStr.length() - 2) : genreStr;
genre.setText(genreStr);
// release year
year.setText(String.valueOf(m.getYear()));
String color = bgColors[position % bgColors.length];
serial.setBackgroundColor(Color.parseColor(color));
return convertView;
}
}
非常感谢任何能够帮助我完全正确行事的人,因为这个小问题几天来一直令我整个项目感到沮丧。
提前致谢!
答案 0 :(得分:1)
"Cannot Resolve Method" getFilter()'
。因为BaseAdapter clas没有方法getFilter()
。但是ArrayAdapter有getFilter()
方法。
您可以在implement filter
上将Custom Listview Adapter with filter Android或How could i filter the listview using baseadapter stackoverflow帖子引荐至BaseAdapter class
,或者如果可能,您可以从BaseAdapter
切换到ArrayAdapter
。
答案 1 :(得分:1)
public class AdapterClass extends BaseAdapter implements Filterable
{
public Filter getFilter() {
return new Filter(){
@Override
protected Filter.FilterResults performFiltering(CharSequence constraint)
{
//This function performs filtering in worker thread
// And return Filter.FilterResults
}
@Override
protected void publishResults(CharSequence constraint, Filter.FilterResults results)
{
// update list from filter Result
// and call notifyDataSetChanged();
}
};
}
}
答案 2 :(得分:0)
在文本中更改了以下方法的调用并将此方法放在适配器上,请始终在电影arraylist中查看“searchText”
// Filter Class
private void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
searchText = charText;
Log.i("installed apps==", searchText);
appsList.clear();
if (charText.length() == 0) {
appsList.addAll(arraylist);
} else {
for (AppPojo wp : arraylist) {
if (wp.getappName().toLowerCase(Locale.getDefault()).contains(charText)) {
appsList.add(wp);
}
}
}
notifyDataSetChanged();
}