Carlos Henrique Callejas Carlos Henrique Callejas - 1 year ago 91
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 = "";
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
Movie movie;

public HomeFragment() {
// Required empty public constructor

public void onCreate(Bundle savedInstanceState) {


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) {

listView = (ListView) getActivity().findViewById(;
adapter = new CustomListAdapter(getActivity(), movieList);

// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
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();

// adding movie to movies array
} catch (JSONException e) {

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

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


// Adding request to request queue

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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(),

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




public void onAttach(Activity activity) {

public void 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;

public int getCount() {
return movieItems.size();

public Object getItem(int location) {
return movieItems.get(location);

public long getItemId(int position) {
return position;

public View getView(int position, View convertView, ViewGroup parent) {

if (inflater == null)
inflater = (LayoutInflater) activity
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);

if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
TextView title = (TextView) convertView.findViewById(;
TextView rating = (TextView) convertView.findViewById(;
TextView genre = (TextView) convertView.findViewById(;
TextView year = (TextView) convertView.findViewById(;

// getting movie data for the row
Movie m = movieItems.get(position);

// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

// title

// rating
rating.setText("Rating: " + String.valueOf(m.getRating()));

// genre
genre.setText("Genero: " + String.valueOf(m.getGenre()));

// release year

return convertView;

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

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 .
//Sets the filter result in variable FilterResults
results.count = itens_filtrados.size();
results.values = itens_filtrados;
return results;

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download