如何在ListView中使用毕加索

时间:2018-06-29 01:51:06

标签: android listview android-adapter picasso simpleadapter

当前,我正在使用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);

2 个答案:

答案 0 :(得分:0)

您可以扩展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);

答案 1 :(得分:0)

尝试一下:

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