[enter image description here](https://i.stack.imgur.com/w4Dqq.png)
-------------------------------------------------------------------

Not sure how clear it may be, but the issue I am having is that my adaptor is adding the same position from a TreeMap multiple times. This shouldn't be happening/ It's adding the same movie multiple times.

I have looked at the code and tried to follow the cycle of the code but I just can't seem to find the problem.

I have made Gists of my code as it's too long and I don't want to spam the page.

If you guys really want it all on here I will update my question.

My application makes requests to an API and gets a TreeMap of movie ids, titles, info, posters... and so on. This particular problem is with my ListViewAdaptor.

[MainActivityFragment.java](https://gist.github.com/sebi1995/67ca1ee79537e081ccb6d16e6288026e)

[ListViewAdaptor.java](https://gist.github.com/sebi1995/49baa2a2eb51b5529234958e493ce7d2)

[MainActivity.java](https://gist.github.com/sebi1995/9f281a86019fc3aedb1a66d08bac02d5)

[fragment\_main.xml](https://gist.github.com/sebi1995/21b87bfa6eb8f3f5564db42b2b6c43dc)

[activity\_main.xml](https://gist.github.com/sebi1995/bd1b11b90f33134a84f65a5d3aeb3a75)

[results\_layout.xml](https://gist.github.com/sebi1995/d53abf5bfb7ed53660cb1dae0f89d074)

[HttpHandler.java](https://gist.github.com/sebi1995/87222a22d3306fd96a8e87cac0a3eeb5)

ListViewAdaptor

```
package com.example.zdroa.fetchmovies;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.TreeMap;

class ListViewAdaptor extends BaseAdapter {

    private Context mContext;
    private TreeMap<Integer, String> urls;
    private TreeMap<Integer, String> titles;

    ListViewAdaptor(Context context, TreeMap<Integer, String> URLS, TreeMap<Integer, String> TITLES) {
        mContext = context;
        urls = URLS;
        titles = TITLES;
    }

    @Override
    public int getCount() {
        return urls.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

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

        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.results_layout, null);
        }
        ImageView imageView = (ImageView) convertView.findViewById(R.id.ivPosters);
        TextView textView = (TextView) convertView.findViewById(R.id.results_layout_tv_movie_name);

        Drawable d = mContext.getResources().getDrawable(R.drawable.place_holder_img);

        String link_end = urls.ceilingEntry(position).getValue();
        String title = titles.ceilingEntry(position).getValue();

        Picasso.with(mContext)
                .load("http://image.tmdb.org/t/p/w185" + link_end)
                .placeholder(d)
                .into(imageView);

        textView.setText(title);

        return convertView;
    }
}

```

MainActivityFragment.java

```
package com.example.zdroa.fetchmovies;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.TreeMap;

public class MainActivityFragment extends Fragment {

    private static String API_KEY = "b692b9da86f1cf0c1b623ea6e2770101";

    ListView listView;
    Button button;

    static TreeMap<Integer, Integer> IDS;
    static TreeMap<Integer, String> TITLES;
    static TreeMap<Integer, String> POSTERS;
    static TreeMap<Integer, String> GENRES;
    static TreeMap<Integer, String> PRODUCTION_COUNTRIES;
    static TreeMap<Integer, String> OVERVIEW;
    static TreeMap<Integer, String> RUNTIME;

    public MainActivityFragment() {
    }

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

        listView = (ListView) rootView.findViewById(R.id.lvMovies);

        new ImageLoadTask().execute();

        //if fragment is visible
        if (getActivity() != null) {

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    System.out.println(TITLES.ceilingEntry(position).getValue());
                }
            });
        }
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    private class ImageLoadTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            IDS = new TreeMap<>();

            IDS.put(1, 2);
            IDS.put(2, 3);
            IDS.put(3, 5);
            IDS.put(4, 6);
            IDS.put(5, 8);
            IDS.put(6, 9);
            IDS.put(7, 11);
            IDS.put(8, 12);
            IDS.put(9, 13);
            IDS.put(10, 14);

            populateTreeMaps();

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (getActivity() != null) {
                ListViewAdaptor adapter = new ListViewAdaptor(getActivity(), POSTERS, TITLES);
                listView.setAdapter(adapter);

                System.out.println(POSTERS);
                System.out.println(TITLES);
            }
        }

        private Void populateTreeMaps() {
            HttpHandler httpHandler = new HttpHandler();

            TITLES = new TreeMap<>();
            POSTERS = new TreeMap<>();

            for (int i = 0; i < IDS.size(); i++) {
                String jsonString = httpHandler.makeServiceCall("https://api.themoviedb.org/3/movie/" + IDS.ceilingEntry(i).getValue() + "?api_key=" + API_KEY);

                if (jsonString != null) {
                    try {
                    //POPULATE ALL THE GRAPHS HERE
                        getTitles(jsonString, IDS.ceilingEntry(i).getValue());
                        getPosterURLs(jsonString, IDS.ceilingEntry(i).getValue());

                    } catch (JSONException e) {
                        return null;
                    }
                }
            }
            return null;
        }

        private void getTitles(String JSONString, Integer mov_id) throws JSONException {
            JSONObject jsonObject = new JSONObject(JSONString);

            String title = jsonObject.getString("title");

            TITLES.put(mov_id, title);
        }

        private void getPosterURLs(String JSONString, Integer mov_id) throws JSONException {
            JSONObject jsonObject = new JSONObject(JSONString);

            String url = jsonObject.getString("poster_path");

            POSTERS.put(mov_id, url);
        }

        private void getYoutubeLink() {
        }
    }
}

```