HashMap具有重复项并导致gridview

时间:2015-07-29 21:09:17

标签: java android gridview hashmap adapter

所以我有一个DownloadJSON函数,我从中获取图像网址。这些被放入一个hashmap并传递给我的gridviewAdapter。这里hashmap中没有重复项。但是,当我从gridviewAdapter打印到控制台时,它会保留重复值。当我开始将图像加载到gridview中时,当然有重复。 如何从散列图中删除这些重复值以及它们为什么出现?

DownloadJSON:

   // Downloading data asynchronously
        private class DownloadJSON extends AsyncTask<String, String, String> {

            @Override
            protected String doInBackground(String... url) {
                json = JSONfunctions.getJSONfromURL(myimageurl);

                try {


                    // Get the array of movies
                    results = json.getJSONArray(TAG_MOVIES);

                    // loop through all the movies
                    for (int i = 0; i < results.length(); i++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        JSONObject r = results.getJSONObject(i);

                        String id = r.getString(TAG_ID);
                        String title = r.getString(TAG_TITLE);
                        String poster =  r.getString(TAG_POSTER); 
                        String release = r.getString(TAG_RELEASE);
                        String vote = r.getString(TAG_VOTE_AVG);
                        String voteCount = r.getString(TAG_VOTE_COUNT);
                        String overview = r.getString(TAG_OVERVIEW);




                        map.put(TAG_ID, id);
                        map.put(TAG_TITLE, title);
                        map.put(TAG_POSTER, poster);
                        map.put(TAG_RELEASE, release);
                        map.put(TAG_VOTE_AVG, vote);
                        map.put(TAG_VOTE_COUNT, voteCount);
                        map.put(TAG_OVERVIEW, overview);

                        mylist.add(map);

         // mylist contains no duplicate values
                         Log.v("JSON", "output " + map); // remove before release
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return null;
            }


            @Override
            protected void onPostExecute(String result) {



               GridView Gridv = (GridView) getActivity().findViewById(R.id.upcoming_gridlayout);

                adapter = new GridViewAdapter(getActivity(), mylist);

                Gridv.setAdapter(adapter);



                super.onPostExecute(result);
            }



        }

这是我的GridViewAdapter:

public class GridViewAdapter extends BaseAdapter {

    Context context;
    ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater = null;
    public Map<String, String> nodublist;
    public ImageView poster;
    public HashMap<String, String> mylist;
    public String url;
    public String posterPath;

    public GridViewAdapter(Context a, ArrayList<HashMap<String, String>> d) {
        context = a;
        data = d;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.upcoming_grid_item, parent, false);


        // find poster ImageView
        poster = (ImageView) vi.findViewById(R.id.upcoming_image);

    //    System.out.println(mylist);

     //   Log.v("mylist", "output " + mylist);

        mylist = new HashMap<>();
        mylist = data.get(position);

      // mylist contains duplicate values now!?


        new DownloadImage().execute();

        return vi;
    }





    public class DownloadImage extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... string) {


            posterPath = mylist.get("poster_path");

            // set image url correctly
            url = "imageExtensionUrl" + posterPath; // sizes 185 width
            return null;

        }

        @Override
        protected void onPostExecute(String result) {

            // load image url into poster
            Picasso.with(context).load(url).into(poster);


            super.onPostExecute(result);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

像这样修改适配器:

public class GridViewAdapter extends BaseAdapter {

Context context;
ArrayList<HashMap<String, String>> data;

public GridViewAdapter(Context a, ArrayList<HashMap<String, String>> d) {
    context = a;
    data = d;
}

public int getCount() {
    return data.size();
}

public HashMap<String, String> getItem(int position) {
    return data.get(position);
}

public long getItemId(int position) {
    return position;
}

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

    if (convertView == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.upcoming_grid_item, parent, false);
     }
    ImageView poster = (ImageView) convertView.findViewById(R.id.upcoming_image);

    HashMap<String, String> mylist = data.get(position);


    String posterPath = mylist.get("poster_path");
    String url = "imageExtensionUrl" + posterPath;
    Picasso.with(context).load(url).into(poster);

    return convertView;
}
}