gpt4 book ai didi

java - Picasso 使用 mysql url 在 arraylist hashmap 中加载图像

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

问题:

我想使用 Picasso 将我的图像 URL 转换为图像。

解释:

使用 JSON 查询从数据库接收数据。数据包含一个我想要转换为图像的 URL(最好使用 Picasso)。

现在做什么:

所有数据都被放入 ArrayList 中。检索数据时,显示除图像之外的所有数据(使用 XML 中的 ImageView)。

我尝试过的

将所有 Picasso 选项放在各处,但我不知道如何在 ArrayList 或 HashMap 中使用 Picasso。

代码

这是我想要实现这个的代码片段。如果需要更多,我会添加更多。

OnCreate之前的一段代码

ArrayList<HashMap<String, String>> recipesList;

加载列表的代码段

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);

// Hashmap for ListView
recipesList = new ArrayList<HashMap<String, String>>();

// Loading ingredients in Background Thread
new LoadAllRecipes().execute();

// Get listview
ListView lv = getListView();

检索信息的代码段,包括 picasso 标签

if (success == 1) {
// recipies found
// Getting Array of Ingredients
recipes = json.getJSONArray(TAG_recipes);

// looping through all recipes
for (int i = 0; i < recipes.length(); i++) {
JSONObject c = recipes.getJSONObject(i);

// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String photo = c.getString(TAG_PHOTO);
String price = c.getString(TAG_PRICE);
String time = c.getString(TAG_TIME);

// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_PRICE, price);
map.put(TAG_TIME, time);
Picasso.get().load(map.put( TAG_PHOTO, photo ));

// adding HashList to ArrayList
recipesList.add(map);

}

底部的适配器

  /**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter;
adapter = new SimpleAdapter(
ActivityTwo.this, recipesList,
R.layout.activity_two_details_cards, new String[] { TAG_PID,
TAG_NAME, TAG_PRICE, TAG_TIME, TAG_PHOTO},
new int [] { R.id.pid, R.id.name, R.id.price, R.id.time, R.id.imagePhoto});// updating listview
setListAdapter(adapter);
}

最佳答案

因为map.put返回

the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)

https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#put(K,%20V)

在您的示例中,特定键的先前映射值为空。您可以将代码修复为

...
map.put(TAG_PHOTO, photo);
Picasso.get().load(map.get(TAG_PHOTO));
...

关于java - Picasso 使用 mysql url 在 arraylist hashmap 中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55102568/

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