我怎样才能将毕加索插入适配器

How can i insert picasso into an adapter

我正在构建一个带有列表视图的 android 应用程序,我想将来自不同 URL 的图像放入我的列表视图中。我正在从我的网站服务器获取图像,就像这样。

www.mywebsite.com/image1.jpg

图像的名称和扩展名位于 json 文件中。

我听说毕加索做的正是我想要的。我只需要这样做:

//Initialize ImageView
ImageView imageView = (ImageView) findViewById(R.id.imageView);

//Loading image from below url into imageView

Picasso.with(this)
   .load("URL HERE")
   .into(imageView);

但是,我正在将数据传递给适配器,但我不知道如何使 Picasso 代码适应我的适配器代码。

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    // Dismiss the progress dialog
    if (pDialog.isShowing())
        pDialog.dismiss();
    /**
     * Updating parsed JSON data into ListView
     * */
    ListAdapter adapter = new SimpleAdapter(
            ListUsersActivity.this, contactList,
            R.layout.list_row, new String[] { TAG_NAME }, new int[] { R.id.name,
                    });

    setListAdapter(adapter);
}

如您所见,我已经将用户名传递给名为 name 的 TextView,现在,我想将 PICTURE_NAME 传递给名为 avatar 的 ImageView。我该怎么做?

谢谢。

您不能 "pass Picasso" 到适配器。您必须创建自己的自定义适配器,这并不像听起来那么令人生畏。它甚至可能基于 SimpleAdapter。像这样:

public class MyAdapter extends SimpleAdapter{

   public MyAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to){
      super(context, data, resource, from, to);
}

   public View getView(int position, View convertView, ViewGroup parent){
      // here you let SimpleAdapter built the view normally.
      View v = super.getView(position, convertView, parent);

      // Then we get reference for Picasso
      ImageView img = (ImageView) v.getTag();
      if(img == null){
         img = (ImageView) v.findViewById(R.id.imageOrders);
         v.setTag(img); // <<< THIS LINE !!!!
      }
      // get the url from the data you passed to the `Map`
      String url = ((Map)getItem(position)).get(TAG_IMAGE);
      // do Picasso
      Picasso.with(v.getContext()).load(url).into(img);

      // return the view
      return v;
   }
}

那么你可以只使用这个 class 而没有参数上的图像(但它必须仍然存在于 orderList 中)。

ListView list= (ListView) getActivity().findViewById(R.id.list);
ListAdapter adapter = 
       new MyAdapter(
                getActivity(),
                orderList,
                R.layout.order_usa_row,
                new String[]{TAG_PRICE,TAG_TITLE,TAG_PSTATUS,TAG_PRICESYMBOL},
                new int[]{R.id.price,R.id.title,R.id.pstatus,R.id.symbol});
list.setAdapter(adapter);