0

在对此投反对票之前请多多包涵。我是 Android 的初学者。在此处尝试多种解决方案后,我未能解决此问题。

我正在尝试使用以下项目在列表视图中设置图像。我希望能够在计划适配器类的某处插入图像代码。

以下代码是我的计划适配器

public class Schedule_ArrayAdapter extends ArrayAdapter<String> {
    private final Activity activity;
    private final String[] name, category, image;
    Typeface colab, colab_bold, Bebas;
    int selected = -1;

public Schedule_ArrayAdapter(Activity activity, String[] name, String[] category, String[] image) {
    super(activity, R.layout.schedule_item, category);
    this.activity = activity;
    this.name = name;
    this.category = category;
    this.image = image;


    this.colab = Typeface.createFromAsset(activity.getAssets(), "ColabThi.otf");
    this.colab_bold = Typeface.createFromAsset(activity.getAssets(), "ColabMed.otf");
    this.colab_bold = Typeface.createFromAsset(activity.getAssets(), "BebasNeue.otf");
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //get view and textview from xml
    View rowView = inflater.inflate(R.layout.schedule_item, parent, false);
    rowView.setBackgroundColor(0xffffffff);
    LinearLayout background = (LinearLayout) rowView.findViewById(R.id.back);
    if (selected != -1) {
        if (selected == position) {
            background.setBackgroundColor(0xffeaac4b);
        } else {
            background.setBackgroundColor(0xffffffff);
        }
    }else{
        background.setBackgroundColor(0xffffffff);
    }

    TextView TimeView = (TextView) rowView.findViewById(R.id.category);
    TextView TitleView = (TextView) rowView.findViewById(R.id.title);
    ImageView vi = (ImageView) rowView.findViewById(R.id.imgPreview);

    GetImage getimage = new GetImage();
    getimage.execute(image);

    //change names
    TitleView.setText(category[position]);
    TitleView.setTextSize(fontpercent_screenheight(3.5));
    TitleView.setPadding(dp(10), dp(5), dp(5), dp(0));
    TitleView.setTypeface(Bebas);

    TimeView.setText(name[position]);
    TimeView.setTextSize(fontpercent_screenheight(3.5));
    TimeView.setPadding(dp(10), dp(2), dp(5), dp(5));
    TimeView.setTypeface(colab);

    return rowView;
}

以下是我的 ImageDownloader 类

public class ImageDownloader {
    private InputStream OpenHttpConnection(String image) throws IOException {
    InputStream in = null;
    int response = -1;

    URL url = new URL(image);
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))
        throw new IOException("Not an HTTP connection");

    conn.setUseCaches(true);
    HttpURLConnection httpConn = (HttpURLConnection) conn;
    try {
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();
        response = httpConn.getResponseCode();
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();
        }
    } catch (Exception ex) {
        throw new IOException("Error connecting" + ex);
    }
    return in;
}

public Bitmap DownloadImage(String image) {
    Bitmap bitmap = null;
    InputStream in = null;
    try {
        in = OpenHttpConnection(image);
        bitmap = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return bitmap;
}
}

解析 JSON 和获取数据正常工作。在调试器中查看时,“图像”数组也被获取。如何在视图中设置图像? 在此处输入图像描述

4

2 回答 2

0

你也可以试试这个代码

ImageView iv=(ImageView) findViewById(R.id.imageview);
    try {
        iv.setImageDrawable(grabImageFromUrl(Global.a5));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

从 URL 获取图像的功能是

    private Drawable grabImageFromUrl(String url) throws Exception {
        return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src");  }
于 2013-05-20T11:28:12.263 回答
0

因此,在您的 getView 中,adapter您应该向某个 url 发出请求并使用

位图位图=BitmapFactory.decodeStream(inputstream);

现在在布局的图像视图中设置它

ImageView i=(ImageView)findViewById(R.id.imageiew);
i.setImageResource(bitmap);

以便显示图像。

于 2013-05-18T06:27:54.123 回答