Carlos Henrique Callejas Carlos Henrique Callejas - 5 months ago 39
JSON Question

update position listview json

Good morning , I'm having problems in updating the position of a listview that receives data via json . I'm able to filter the information , but at the time of passing information from one list item to another activity he simply passes the item of the position that was before filtering.

This is my class that receives the listview:

public class HomeFragment extends Fragment {

// Log tag
private static final String TAG = MainActivity.class.getSimpleName();

// Movies json url
private static final String url = "http://api.androidhive.info/json/movies.json";
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
Movie movie;

public HomeFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);


// Inflate the layout for this fragment

return rootView;
}

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

listView = (ListView) getActivity().findViewById(R.id.list);
adapter = new CustomListAdapter(getActivity(), movieList);
listView.setAdapter(adapter);

// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());

// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(obj.getString("rating"));
movie.setGenre(obj.getString("genre"));
movie.setYear(obj.getString("releaseYear"));

// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}

}
// notifying list adapter about data changes
// so that it renders the list view with updated data

//calls getFilter
adapter.getFilter().filter("The");
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());

}
});

// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
movie = movieList.get(position);
String thumbNail = movie.getThumbnailUrl();
String titulo = movie.getTitle();
String pontuacao = movie.getRating();
String genero = movie.getGenre();
String ano = movie.getYear();

// Starting single contact activity
Intent in = new Intent(getActivity(),
SearchResultsActivity.class);

in.putExtra("img", image);
in.putExtra("title", title);
in.putExtra("pontuacao", rating);
in.putExtra("genero", genre);
in.putExtra("ano", year);

startActivity(in);

}
});

}


@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}

@Override
public void onDetach() {
super.onDetach();
}


This is my adapter class:

public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
private List<Movie> mMovieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public CustomListAdapter(Activity activity, List<Movie> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
this.mMovieItems = movieItems;
}

@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, null);

if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
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
genre.setText("Genero: " + String.valueOf(m.getGenre()));

// release year
year.setText(String.valueOf(m.getYear()));

return convertView;
}

public Filter getFilter() {
Filter filter = new Filter() {

@Override
protected FilterResults performFiltering(CharSequence filter) {
FilterResults results = new FilterResults();
//if it was not carried out no filter inserts all items.
if (filter== null || filter.length() == 0) {
results.count = mMovieItems.size();
results.values = mMovieItems;
} else {
//creates an array to store the filtered objects.
List<Movie> itens_filtrados = new ArrayList<Movie>();

//runs list checking that contains the filter word in the description of the obje
for (int i = 0; i < mMovieItems.size(); i++) {
Movie data = mMovieItems.get(i);

filter= filter.toString().toLowerCase();
String condicao = data.getTitle().toLowerCase();

if (condicao.contains(filter)) {
//restrain adds to the list of filtered items .
itens_filtrados.add(data);
}
}
//Sets the filter result in variable FilterResults
results.count = itens_filtrados.size();
results.values = itens_filtrados;
}
return results;
}

@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
movieItems = (List<Movie>) results.values; //filtered values.
notifyDataSetChanged(); //Notifies the change list
}
};
return filter;
}


I hope someone can help me , thank you in advance.

Answer

In your onItemClick, you are getting the item from your full list rather than the filtered one.

Instead of:

movie = movieList.get(position);

You should get the item from the adapter as follows.

movie = (Movie) adapter.getItem(position);

This should return the item in the list of filtered items because your publishResults sets the filtered items list to movieItems.