【问题标题】:Picasso load image in arraylist hashmap using mysql urlPicasso使用mysql url在arraylist hashmap中加载图像
【发布时间】:2019-08-01 19:25:05
【问题描述】:

问题:

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

解释:

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

现在做了什么:

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

我尝试了什么

把毕加索的所有选项都放在各处,但我不知道如何在 ArrayList 或 HashMap 中使用毕加索。

代码

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

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

检索信息的一段代码,包括毕加索标签

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

【问题讨论】:

    标签: java arraylist hashmap picasso


    【解决方案1】:

    因为map.put返回

    与键关联的前一个值,如果没有,则返回 null 键的映射。 (空返回也可以表示地图 如果实现支持,则以前将 null 与键关联 空值。)

    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));
    ...
    

    【讨论】:

    • 感谢您,但这并没有解决问题。 logcat 出错:W/ImageView: resolveUri failed on bad bitmap uri: http://...
    • @Koetjeboe 你在真实设备上调试你的应用程序吗?
    • 我使用虚拟设备。我也会把它移植到我的手机上。编辑:不,真实设备上也没有图像。
    • @Koetjeboe 尝试通过互联网浏览器从设备打开您的图像 http://.. 以检查可访问性。这似乎是毕加索没有的问题
    • 图片完美运行。在另一个选项卡上(单击配方时)毕加索完美加载图像(普通页面,而不是数组列表)
    猜你喜欢
    • 2017-05-13
    • 2019-08-20
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    相关资源
    最近更新 更多