如何在列表视图中使用毕加索
Posted
技术标签:
【中文标题】如何在列表视图中使用毕加索【英文标题】:How to use picasso in listview 【发布时间】:2018-12-08 03:33:28 【问题描述】:目前我正在使用 sipleAdapter() 创建一个适配器并放入 listView。但我想在该列表视图中放置一个图像,因此解决方案是创建自定义适配器,但在自定义适配器 (BeritaAdapter) 中,我的毕加索无法使用 () 解析方法。
Picasso.with(context).load(url).into(img);
这是我的 Berita 课程
public class Berita extends AppCompatActivity
ListView listview;
ArrayList<HashMap<String,String>> beritaList;
String id_kategori;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.berita);
Intent i = getIntent();
id_kategori = i.getStringExtra("id_kategori");
beritaList = new ArrayList<HashMap<String, String>>();
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://192.168.166.2/android/portal/berita/kategori";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
@Override
public void onResponse(String response)
// response
Log.d("hasil", response.toString());
try
JSONObject responobjek = new JSONObject(response);
JSONArray jsonArray = responobjek.getJSONArray("berita");
Log.d("hasil jsonarray", jsonArray.toString());
for (int i = 0; i < jsonArray.length(); i++)
JSONObject c = jsonArray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("id_berita", c.getString("id_berita"));
map.put("judul_berita", c.getString("judul_berita"));
map.put("gambar_berita", c.getString("gambar_berita"));
beritaList.add(map);
String[] keys = "id_berita", "judul_berita";
int[] ids = R.id.id_berita, R.id.judul_berita;
//MY CUSTOM ADAPTER
BeritaAdapter adapter = new BeritaAdapter(Berita.this, beritaList, R.layout.list_item_berita, keys, ids);
listview.setAdapter(adapter);
catch (JSONException e)
e.printStackTrace();
,new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
// error
Log.d("Error.Response", "embuh error opo");
)
@Override
protected Map<String, String> getParams()
Map<String, String> params = new HashMap<String, String>();
params.put("id_kategori", id_kategori);
return params;
;
queue.add(postRequest);
在该脚本中,我使用的是我的自定义适配器,即 BeritaAdapter()。 这里是 BeritaAdapter 文件
public class BeritaAdapter extends SimpleAdapter
Context context;
public BeritaAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
super(context, data, resource, from, to);
this.context = context;
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 = v.findViewById(R.id.gambar_berita);
// get the url from the data you passed to the `Map`
String url = (String) ((Map)getItem(position)).get("gambar_berita");
Picasso.with(context).load(url).into(img);
// return the view
return v;
如何获取上下文以便我可以输入
Picasso.with(context).load(url).into(img);
【问题讨论】:
您已经有了上下文,那么您的问题是什么?对不起,因为我还不明白 所以请显示错误日志 我之前正在学习一些教程,并且我已经有了上下文,但是在 BeritaAdapter.java 中我发现错误无法解析方法'with(android.content.Context)'。我正在尝试使用 .get() 替换,但我仍然面临同样的问题 你的毕加索版本是多少? 这里是我的依赖实现 'com.squareup.picasso:picasso:2.71828' 【参考方案1】:试试这个:
@Override
public View getView(int position, View convertView, ViewGroup parent)
if (null == convertView)
convertView = inflater.inflate(R.layout.listview_item_image, parent, false);
Picasso
.with(context)
.load(imageUrls[position])
.fit() // will explain later
.into((ImageView) convertView);
return convertView;
【讨论】:
【参考方案2】:您可以扩展 BaseAdapter 构造函数并请求 Context 作为对象参数。 然后,在初始化过程中,可以使用关键字“this”、getApplicationContext 等。
public class BeritaAdapter extends SimpleAdapter
Context context;
public BaritaAdapter(Context context)
this.context = context;
public BeritaAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
super(context, data, resource, from, to);
this.context = context;
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 = v.findViewById(R.id.gambar_berita);
// get the url from the data you passed to the `Map`
String url = (String) ((Map)getItem(position)).get("gambar_berita");
Picasso.with(context).load(url).into(img);
// return the view
return v;
那么,
BeritaAdapter mAdapter = new BeritaAdapter(this);
或者
Berita Adapter mAdapter = new BeritaAdapter(getApplicationContext);
【讨论】:
面临新问题,我已经使用了 2 个构造函数 this.context = context; public BeritaAdapter(Context context, List extends Map以上是关于如何在列表视图中使用毕加索的主要内容,如果未能解决你的问题,请参考以下文章