题:
我想使用Picasso将我的图片网址转换为图片。
解释:
使用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();
检索信息的代码片段,包括毕加索标签
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返回
与key关联的先前值,如果没有key的映射,则返回null。 (如果实现支持空值,则null返回也可以指示映射先前与key关联null。)
https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#put(K,%20V)
在您的示例中,特定键的上一个映射值为null。你可以修改你的代码
...
map.put(TAG_PHOTO, photo);
Picasso.get().load(map.get(TAG_PHOTO));
...