gpt4 book ai didi

java - HashMap 有重复并导致 gridview 中图像重复

转载 作者:太空宇宙 更新时间:2023-11-04 13:35:46 24 4
gpt4 key购买 nike

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

下载JSON:

   // 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);
}
}
}

最佳答案

像这样修改适配器:

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;
}
}

关于java - HashMap 有重复并导致 gridview 中图像重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31711160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com