I have two questions here.
The First Question goes :: After i implementing
Filterable
interface in my CustomListAdapter to enable search on list data, i realized that each time i try to reload the listview, it refused to populate the listview even thought i was able to fetch the data from my server. (My ListView always reloads on device orientation change and i also added a reload button to reload the listView onclick too)The Second Question goes :: My search EditView populates my listView very well onchange but i realized that when i empty the EditView, the previous data in the ListView does not revert/come-back to its previous content
Below is my Activity Code
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);
}
}
And here is the code in my 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();
}
}
}
As you see i really took my time to put this whole code together but at this junction, my head spins as i cant figure out why
On orientation change, the listView data which use to rePopulate before suddenly stops populating after i introduced the Filterable to my CustomListAdapter class and also
When i clear the search EditView, the content in the ListView does not return to
When i click on the reload button, the content in the list does not reload, just like it does when the device orientation changes.
I would really appreciate a hint to help me out as i am so so stuck right now.
Thanks in Advance
-Edited CustomListAdapter the changes i made are
@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();
}
}
also edited this too. Added 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);
}
Added this method to CustomListAdapter
//Add Below Method
public void reloadData(){
this.originalMovieList = new ArrayList<Movie>(movieItems);
notifyDataSetChanged();
}
Also replaced notifyDataSetChanged() in MainActivity Class with this
`// notifying list adapter about data changes
// so that it renders the list view with updated data
//adapter.notifyDataSetChanged();
adapter.reloadData();`
-Reload Button now works fine, Orientation change reloads the page well but the Search does not populate the ListView. Thus, Search not working yet