gpt4 book ai didi

android - 搜索 ListView 标志的功能 - 无法解析方法“getFilter()”

转载 作者:行者123 更新时间:2023-11-29 20:18:28 26 4
gpt4 key购买 nike

我一直在尝试实现对 listView 的简单搜索,我已经能够使用 Volley 填充它,但直到现在都无济于事。它一直标记这个 Cannot resolve method '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
}
});




}



}

这是很好地填充我的 listView 的 CustomListAdapter 代码

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;
}
}

如果有人能帮助我解决我做错的地方,我将不胜感激,因为这个小问题已经困扰我整个项目好几天了。

提前致谢!

最佳答案

“无法解析方法”getFilter()'。因为BaseAdapter类没有方法 getFilter()。但是ArrayAdaptergetFilter() 方法。

可以引用Custom Listview Adapter with filter AndroidHow could i filter the listview using baseadapter stackoverflow 发布到 BaseAdapter 类 上的 implement filter,或者如果可能,您可以从 BaseAdapter 切换到 ArrayAdapter

关于android - 搜索 ListView 标志的功能 - 无法解析方法“getFilter()”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33521687/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com