在我的 CustomListAdapter 上实现 Filterable 接口后无法重新加载 ListView 内容
Posted
技术标签:
【中文标题】在我的 CustomListAdapter 上实现 Filterable 接口后无法重新加载 ListView 内容【英文标题】:Can not reload ListView Content after implementing Filterable interface on my CustomListAdapter 【发布时间】:2016-02-05 09:53:00 【问题描述】:我有两个问题。
第一个问题是 :: 在我在 CustomListAdapter 中实现 Filterable
接口以启用对列表数据的搜索后,我意识到每次尝试重新加载列表视图时,它都会拒绝填充列表视图甚至认为我能够从我的服务器获取数据。 (我的 ListView 总是在设备方向更改时重新加载,我还添加了一个重新加载按钮来重新加载 listView onclick)
第二个问题去 :: 我的搜索 EditView 很好地填充了我的 listView onchange 但我意识到当我清空 EditView 时,ListView 中的先前数据不会恢复/来-回到之前的内容
下面是我的活动代码
public class MainActivity extends Activity
private ListView mList;
private List<Movie> movieList = new ArrayList<Movie>();
EditText inputSearch;
private CustomListAdapter adapter;
private ImageView reload_img;
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);
reload_img = (ImageView) findViewById(R.id.reload_list);
adapter = new CustomListAdapter(this, movieList);
mList.setAdapter(adapter);
//FETCH Movie-Data FROM SERVER TO LIST
fetchMovie();
//Reload image click
reload_img.setOnClickListener(new View.OnClickListener()
public void onClick(View arg0)
reload_list();
);
//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
);
//RELOAD LISTVIEW METHOD
private void reload_list()
pDialog = new ProgressDialog(MainActivity.this);
// Showing progress dialog before making http request
pDialog.setMessage("Synchronising....");
pDialog.show();
pDialog.setCancelable(false);
pDialog.setCanceledOnTouchOutside(false);
flag_loading = true;
offSet = 0;
inputSearch.setText("");
movieList.clear();
fetchMovie();
//adapter.notifyDataSetChanged();
//LOAD DATA FROM SERVER TO LISTVIEW USING VOLLEY
private void fetchMovie()
// showing refresh animation before making http call
//swipeRefreshLayout.setRefreshing(true);
// appending offset to url
String url = Config.URL_TOP_250_URL + offSet;
//String url = Config.MOVIE_JSON_URL;
//String url = Config.MOVIE_JSON_URL_SHORT;
// Toast.makeText(MainActivity.this,
// "::Json loaded:: ", Toast.LENGTH_SHORT).show();
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>()
@Override
public void onResponse(JSONArray response)
// Log.d(TAG, response.toString());
hidePDialog();
if (response.length() > 0)
//Toast.makeText(MainActivity.this,
// "Json String : \n"+ response.toString(), Toast.LENGTH_LONG).show();
// Parsing json
/**/ for (int i = 0; i < response.length(); i++)
try
JSONObject obj = response.getJSONObject(i);
int rank = obj.getInt("rank");
Movie movie = new Movie();
// movie.setRank(obj.getInt("rank"));
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(((Number) obj.get("rating"))
.doubleValue());
movie.setYear(obj.getInt("releaseYear"));
// Genre is json array
JSONArray genreArry = obj.getJSONArray("genre");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++)
genre.add((String) genreArry.get(j));
movie.setGenre(genre);
// adding movie to movies array
movieList.add(movie);
// updating offset value to highest value
if (rank >= offSet)
offSet = rank;
flag_loading = false;
catch (JSONException e)
e.printStackTrace();
Toast.makeText(MainActivity.this,
"Network connection error \n "+ e.getMessage(), Toast.LENGTH_LONG).show();
/**/
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
else
Toast.makeText(MainActivity.this,
"End of movie-list" , Toast.LENGTH_LONG).show();
, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
VolleyLog.d(TAG, "Error :: " + error.getMessage());
hidePDialog();
);
// Adding request to request queue
MyApplication.getInstance().addToRequestQueue(movieReq);
这是我的 CustomListAdapter 中的代码
public class CustomListAdapter extends BaseAdapter
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
private List<Movie> originalMovieList;
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;
this.originalMovieList = 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;
final List<Movie> list = movieItems;
int count = list.size();
// final ArrayList<String> nlist = new ArrayList<String>(count);
final ArrayList<Movie> nlist = new ArrayList<Movie>(count);
String filterableString ;
for (int i = 0; i < count; i++)
filterableString = list.get(i).getTitle();
if (filterableString.toLowerCase().contains(filterString))
// nlist.add(filterableString);
nlist.add(list.get(i));
results.values = nlist;
results.count = nlist.size();
return results;
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
//filteredData = (ArrayList<String>) results.values;
movieItems = (ArrayList<Movie>) results.values;
notifyDataSetChanged();
如你所见,我确实花时间将整个代码放在一起,但在这个路口,我头晕目眩,我不知道为什么
方向更改,在我将 Filterable 引入我的 CustomListAdapter 之后,之前用于重新填充的 listView 数据突然停止填充类,还有
当我清除搜索EditView时,ListView中的内容没有返回
当我点击重新加载按钮时,列表中的内容不会重新加载,就像设备方向改变时一样。
我真的很感激能帮助我的提示,因为我现在很困。
Thanks in Advance
-Edited CustomListAdapter我所做的更改是
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
//filteredData = (ArrayList<String>) results.values;
//movieItems = (ArrayList<Movie>) results.values;
movieItems.clear();
movieItems.addAll((ArrayList<Movie>) results.values);
notifyDataSetChanged();
也编辑了这个。已添加this.originalMovieList = new ArrayList<Movie>(movieItems);
public CustomListAdapter(Activity activity, List<Movie> movieItems)
this.activity = activity;
this.movieItems = movieItems;
// this.originalMovieList = movieItems;
this.originalMovieList = new ArrayList<Movie>(movieItems);
bgColors = activity.getApplicationContext().getResources().getStringArray(R.array.movie_serial_bg);
将此方法添加到 CustomListAdapter
//Add Below Method
public void reloadData()
this.originalMovieList = new ArrayList<Movie>(movieItems);
notifyDataSetChanged();
还将 MainActivity 类中的 notifyDataSetChanged() 替换为 this
`// notifying list adapter about data changes
// so that it renders the list view with updated data
//adapter.notifyDataSetChanged();
adapter.reloadData();`
-重新加载按钮现在可以正常工作,方向更改可以很好地重新加载页面,但搜索不会填充 ListView。 因此,搜索还不能正常工作
【问题讨论】:
尝试在适配器构造函数中将 this.originalMovieList = movieItems; 更改为 this.originalMovieList = new ArrayListthis.originalMovieList = movieItems;
更改为 this.originalMovieList = new ArrayList<Movie>(movieItems);
并且我也修改了以下行 final List<Movie> list = movieItems
现在更改为 final List<Movie> list = originalMovieList;
并更改了方法 movieItems = (ArrayList<Movie>) results.values;
上的 publicResults 代码也更改为 originalMovieList = (ArrayList<Movie>) results.values;
这使得重新加载工作但搜索搜索自动停止工作
final List以下是您的问题的答案:
1。 ListView
没有重新加载?在函数中查看您的代码后:
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
//filteredData = (ArrayList<String>) results.values;
movieItems = (ArrayList<Movie>) results.values;
notifyDataSetChanged();
当然,上述功能使您的适配器无法重新加载。为什么?因为这条线:movieItems = (ArrayList<Movie>) results.values;
。您将适配器中的movieItems
引用到另一个List
,因此movieList.add(movie);
d 在您的MainActivity
中不再更改您的adapter
中的任何内容。如何克服它?修改如下:
movieItems.clear();
movieItems.addAll((ArrayList<Movie>) results.values);
notifyDataSetChanged();
但是,您的Search
功能仍然不起作用。为什么?因为在performFiltering
中,您尝试在movieItems
中搜索,而不是您的备份数据。 --> 你应该在你的备份数据中搜索:originalMovieList
,然后通过添加movieItems
来发布。
//change final List<Movie> list = movieItems;
//to belowed line in peformFiltering
final List<String> list = originalData;
为什么您的适配器没有恢复?因为你控制它,但你不恢复它。您应该在 peformFiltering
函数中恢复它:
protected FilterResults performFiltering(CharSequence constraint)
String filterString = constraint.toString().toLowerCase();
if(TextUtils.isEmpty(filterString))
// do restore backup data here
else
// do filter here.
【讨论】:
非常高兴得到您的帮助和时间。将适用于我的代码并更新您。虽然我之前已经调整了我的 publicResult 方法以拥有movieItems.clear(); movieItems.addAll((ArrayList<Movie>) results.values); notifyDataSetChanged();
。在这个问题中也有更新。
我不知道你在if statement
中所说的// do restore backup data here
到底是什么意思,也不知道你在if statement
中所说的// do filter here.
是什么意思,因为我所尝试的只是在程序上进行标记。 -也许你应该填写if condition
的在此处恢复备份数据方面,以便我可以了解你的意思
当你说我应该使用final List<String> list = originalData;
时,我想相信你在这里犯了一个错误,因为程序中没有声明/实例化任何原始数据。 - 你是说final List<Movie> list = originalMovieList;
吗?
在这个链接上是我更新的代码..pastebin.com/RXGjrg4u它将帮助你了解我的代码现在与我所做的所有更改
@OkechukwuEze 是的,这是我的拼写错误。在do restore backup
中,您应该恢复原始数据,例如:` results.values = originalMovieList;结果.count = originalMovieList.size();`【参考方案2】:
这就是我能够解决搜索代码的痛苦的方法。
在我的 MainActivity
上public class MainActivity extends Activity
private List<Movie> currentMovieList = new ArrayList<Movie>();
private List<Movie> originalMovieList = new ArrayList<Movie>();
private CustomListAdapter adapter;
private ListView mList;
// Search EditText
EditText inputSearch;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
inputSearch = (EditText) findViewById(R.id.inputSearch);
adapter = new CustomListAdapter(this, currentMovieList);
mList.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher()
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3)
// When user changed the Text
String filterString = cs.toString().toLowerCase();
Log.e("TAG", "filterString:" + filterString);
currentMovieList.clear();
if (TextUtils.isEmpty(filterString))
currentMovieList.addAll(originalMovieList);
String filterableString;
for (Movie movie : originalMovieList)
if (movie.getTitle().toLowerCase().contains(filterString))
currentMovieList.add(movie);
else if (String.valueOf(movie.getYear()).toLowerCase().contains(filterString))
currentMovieList.add(movie);
else if (String.valueOf(movie.getRating()).toLowerCase().contains(filterString))
currentMovieList.add(movie);
else if (movie.getGenre().toString().toLowerCase().contains(filterString))
currentMovieList.add(movie);
adapter.notifyDataSetChanged();
//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
public class CustomListAdapter extends BaseAdapter implements Filterable
private Activity activity;
private LayoutInflater inflater;
private List<String>originalData = null;
// private List<String>filteredData = null;
private List<Movie> movieItems;
private List<Movie> originalMovieList;
private String[] bgColors;
ImageLoader imageLoader = MyApplication.getInstance().getImageLoader();
private ItemFilter mFilter = new ItemFilter();
public CustomListAdapter(Activity activity, List<Movie> movieItems)
this.activity = activity;
this.movieItems = movieItems;
// this.originalMovieList = movieItems;
this.originalMovieList = new ArrayList<Movie>(movieItems);
bgColors = activity.getApplicationContext().getResources().getStringArray(R.array.movie_serial_bg);
//Add Below Method
public void reloadData()
this.originalMovieList = new ArrayList<Movie>(movieItems);
notifyDataSetChanged();
@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();
//results.values = nlist;
//results.count = nlist.size();
results.values = originalMovieList;
results.count = originalMovieList.size();
return results;
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
//filteredData = (ArrayList<String>) results.values;
//movieItems = (ArrayList<Movie>) results.values;
movieItems.clear();
movieItems.addAll((ArrayList<Movie>) results.values);
notifyDataSetChanged();
现在这是一个 WRAP。 感谢@DhavalPatel 那家伙是 android 大师。 He made all this happen. -Happy Coding
【讨论】:
以上是关于在我的 CustomListAdapter 上实现 Filterable 接口后无法重新加载 ListView 内容的主要内容,如果未能解决你的问题,请参考以下文章
在我的 .NET Windows 窗体上实现从 Chrome 拖放
如何在我的 Spring 4.0 RestFul Web 服务上实现 Spring Security?
如何在我的 PHP/SQL/HTML/CSS 代码上实现 MVC 样式? [关闭]