搜索功能使用BaseAdapter不能在搜索列表填充的ListView [英] Search Functionality to ListView using BaseAdapter not populating List on search
问题描述
我一直在努力实现一个简单的搜索,我已经能够使用凌空但都无济于事,直到日期来填充一个ListView。由于 @Dhaval帕特尔谁一直是映入眼帘的帮助。但我现在遇到的问题是,当我搜索,在的ListView拒绝改变内容从而使他们的搜索程序中的无用
下面是我的code:
公共类MainActivity延伸活动{ 私人的ListView mList;
私人列表<电影及GT; movieList =新的ArrayList<电影及GT;();
的EditText inputSearch;
保护无效的onCreate(捆绑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);
适配器=新CustomListAdapter(这一点,movieList);
mList.setAdapter(适配器);
//搜索TEXTCHANGE
inputSearch.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效onTextChanged(CharSequence的CS,INT ARG1,ARG2 INT,INT ARG3){
//当用户更改文本
。MainActivity.this.adapter.getFilter()过滤(cs.toString());
//标志不能解析法'用getFilter()'在这里
} @覆盖
公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,INT ARG2,
INT ARG3){
// TODO自动生成方法存根 } @覆盖
公共无效afterTextChanged(编辑为arg0){
// TODO自动生成方法存根
}
});
}}
另外这是code在我CustomListAdapter类
公共类CustomListAdapter延伸BaseAdapter { 私人活动活动;
私人LayoutInflater吹气;
私人列表<电影及GT; movieItems;
私有String [] bgColors;
私人列表<串GT; originalData = NULL;
私人列表<串GT; filteredData = NULL;
私人ItemFilter MFILTER =新ItemFilter();
ImageLoader的ImageLoader的= MyApplication.getInstance()getImageLoader()。
公共CustomListAdapter(活动活动,列表与LT;电影> movieItems){
this.activity =活动;
this.movieItems = movieItems;
bgColors = activity.getApplicationContext()getResources()getStringArray(R.array.movie_serial_bg)。
}
@覆盖
公众诠释的getCount(){
返回movieItems.size();
} @覆盖
公共对象的getItem(INT位置){
返回movieItems.get(位置);
} @覆盖
众长getItemId(INT位置){
返回的位置;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){ 如果(吹气== NULL)
吹气=(LayoutInflater)活动
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
如果(convertView == NULL)
convertView = inflater.inflate(R.layout.list_row_image,NULL); 如果(ImageLoader的== NULL)
ImageLoader的= MyApplication.getInstance()getImageLoader()。
NetworkImageView缩略图=(NetworkImageView)convertView.findViewById(R.id.thumbnail); TextView的串行=(TextView中)convertView.findViewById(R.id.serial);
TextView的标题=(TextView中)convertView.findViewById(R.id.title);
TextView的等级=(TextView中)convertView.findViewById(R.id.rating);
TextView的流派=(TextView中)convertView.findViewById(R.id.genre);
TextView的一年=(TextView中)convertView.findViewById(R.id.releaseYear); //获取电影数据的行
电影M = movieItems.get(位置); //缩略图
thumbNail.setImageUrl(m.getThumbnailUrl(),ImageLoader的); //称号
title.setText(m.getTitle()); // 评分
rating.setText(评级:+将String.valueOf(m.getRating())); // 类型
串genreStr =;
对于(字符串str:m.getGenre()){
genreStr + = STR +,;
}
genreStr = genreStr.length()> 0? genreStr.substring(0,
genreStr.length() - 2):genreStr;
genre.setText(genreStr); //发行年份
year.setText(将String.valueOf(m.getYear())); 串色= bgColors [位置%bgColors.length]。
serial.setBackgroundColor(Color.parseColor(彩色));
返回convertView;
} 公共过滤用getFilter(){
返回MFILTER;
}私有类ItemFilter扩展过滤器{
@覆盖
保护FilterResults performFiltering(CharSequence的约束){ 字符串filterString = constraint.toString()与toLowerCase()。 FilterResults结果=新FilterResults(); 最终名单<串GT;名单= originalData; 诠释计数=则为list.size();
最终的ArrayList<串GT; NLIST =新的ArrayList<串GT;(计数); 串filterableString; 的for(int i = 0; I<计数;我++){
filterableString = list.get(ⅰ);
如果(filterableString.toLowerCase()。包括(filterString)){
nlist.add(filterableString);
}
} results.values = NLIST;
results.count = nlist.size(); 返回结果;
} @燮pressWarnings(未登记)
@覆盖
保护无效publishResults(CharSequence的约束,FilterResults结果){
filteredData =(ArrayList的<串GT;)results.values;
notifyDataSetChanged();
}}}
所以,有人可以告诉我什么我没有做对。
在此先感谢
私有类ItemFilter扩展过滤器{
@覆盖
保护FilterResults performFiltering(CharSequence的约束){ 字符串filterString = constraint.toString()与toLowerCase()。 FilterResults结果=新FilterResults(); 最终名单<电影及GT;名单= movieItems; 诠释计数=则为list.size();
最终的ArrayList<电影及GT; NLIST =新的ArrayList<电影及GT;(计数); 串filterableString; 的for(int i = 0; I<计数;我++){
filterableString = list.get(ⅰ).getName();
如果(filterableString.toLowerCase()。包括(filterString)){
nlist.add(list.get(I));
}
} results.values = NLIST;
results.count = nlist.size(); 返回结果;
}@燮pressWarnings(未登记)
@覆盖
保护无效publishResults(CharSequence的约束,FilterResults结果){
movieItems =(ArrayList的<串GT;)results.values;
notifyDataSetChanged();
}}
I have been trying to implement a simple search to a listView which I have been able to populate using Volley but all to no avail till date. Thanks to @Dhaval Patel who has been of greet help. But the issue I am having now is that when I search, the ListView refuse to change content thus making the whose search useless in the program
Below is my code:
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);
//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.toString());
//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
}
});
}
}
Also this is the code in my CustomListAdapter class
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
private String[] bgColors;
private List<String>originalData = null;
private List<String>filteredData = null;
private ItemFilter mFilter = new ItemFilter();
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;
}
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<String> list = originalData;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
}
So can someone please tell me what I am not doing right. Thanks in advance
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<Movie> list = movieItems;
int count = list.size();
final ArrayList<Movie> nlist = new ArrayList<Movie>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i).getName();
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(list.get(i));
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
movieItems= (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
这篇关于搜索功能使用BaseAdapter不能在搜索列表填充的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!