如何将图像添加到列表视图中

时间:2016-05-31 14:42:19

标签: android listview

您好我想在我的列表视图中添加图片。 我怎样才能做到这一点? xml代码 list.xml

private void showEmployee(String json){
    try {
        JSONArray result = new JSONArray(json);
        JSONObject c = result.getJSONObject(0);
        String ref = c.getString("Reference");
        String loginr = c.getString("Login");
        String prix = c.getString("Prix");
        String quantite = c.getString("Quantite");
        String prixT = c.getString("PrixT");
        String dateCom = c.getString("DateCom");
        editRef.setText(ref);
        editLogin.setText(loginr);
        editprixu.setText(prix);
        editQt.setText(quantite);
        editprix.setText(prixT);
        editdate.setText(dateCom);} 

java代码

我尝试使用评论中的代码,但它不起作用

{{1}}

catch(JSONException e){             e.printStackTrace();}

提前致谢

2 个答案:

答案 0 :(得分:0)

我只知道你应该使用适配器。就像:

public class ImageAdapter extends BaseAdapter{
...
}

然后,就像使用它一样:

listView.setAdapter(new ImageAdapter());

您可以覆盖getView(),如:

@Override
public View getView(int position, View convertView, ViewGroup container) {

    ImageView view = (ImageView) convertView;
    if (view == null) {
        view = new ImageView(context);
    }
    Picasso.with(context).setIndicatorsEnabled(true);
    Picasso.with(context).load((String)getItem(position)).placeholder(R.drawable.loading).error(R.drawable.error).resize(450,600).into(view);
    return view;
    /*
    ImageView imageView;
    if (convertView == null) { // if it's not recycled, initialize some attributes
        imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(450, 600));
    } else {
        imageView = (ImageView) convertView;
    }
    imageView.setImageBitmap(bms[position]); // Load image into ImageView
    return imageView;
    */
}

在此功能中添加布局。也许它会起作用。

答案 1 :(得分:0)

请按照此示例listView with image

相关问题