java - 将图像加载到自定义 ArrayAdapter

标签 java android

我尝试使用 arrayAdapter 自定义将图像和文本加载到 ListView,但加载图像失败

这是我的数组适配器

public class CustomAdapter extends ArrayAdapter<Post> {

    private ImageTask image;

    public CustomAdapter(Context context, ArrayList<Post> posts) {
        super(context, 0, posts);
    }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Post post = getItem(position);    
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
           convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_list, parent, false);
        }
        // Lookup view for data population
        TextView tituloView = (TextView) convertView.findViewById(R.id.titulo);
        TextView subtituloView = (TextView) convertView.findViewById(R.id.subTitulo);
        ImageView fotoView = (ImageView) convertView.findViewById(R.id.imageView);
        TextView textoView = (TextView) convertView.findViewById(R.id.texto);


        // Populate the data into the template view using the data object
        tituloView.setText(post.post_titulo);
        subtituloView.setText(post.post_sub_titulo);
        //this like idk if are the way corret for load the image into ViewImage specific
        new ImageTask(fotoView).execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");

        textoView.setText(post.post_texto);
        // Return the completed view to render on screen
        return convertView;
    }
}

这里是 ImageTask 图像

public class ImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public ImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

这是来自 MainActivity 的调用

// Create the adapter to convert the array to views
CustomAdapter adapter = new CustomAdapter(this, arrayOfPost);
// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.lvUsers);
listView.setAdapter(adapter);

arrayOfPost 是解析 jonson 到 ArrayList 的结果,链接图像包含在其中,但在本例中我使用来自 google 开发人员的图像

以及此处数组适配器使用的 xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <TextView 
            android:id="@+id/titulo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/subTitulo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/texto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
</LinearLayout>

有人知道我的错误是什么以及我应该如何加载图像吗? 我不知道是否是在特定 ViewImage 中加载图像的正确方式 //这几行是一流的发布

new ImageTask(fotoView).execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");

最佳答案

在 AsyncTask 中加载图像的步骤

第 1 步:

ImageView fotoView = (ImageView) convertView.findViewById(R.id.imageView);

第 2 步:

String URL1 = "http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png";

第 3 步:

fotoView.setTag(URL1);
new DownloadImageTask.execute(fotoView);

第 4 步:

public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {

ImageView imageView = null;

@Override
protected Bitmap doInBackground(ImageView... imageViews) {
    this.imageView = imageViews[0];
    return download_Image((String)imageView.getTag());
}

@Override
protected void onPostExecute(Bitmap result) {
    imageView.setImageBitmap(result);
}

private Bitmap download_Image(String url) {

    Bitmap bmp =null;
    try{
        URL ulrn = new URL(url);
        HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
        InputStream is = con.getInputStream();
        bmp = BitmapFactory.decodeStream(is);
        if (null != bmp)
            return bmp;

        }catch(Exception e){}
    return bmp;
} }

引用:Android : Loading an image from the Web with Asynctask

关于java - 将图像加载到自定义 ArrayAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31039741/

相关文章:

java - eclipse/android/java - 无法从库中的 jar 文件引用类

java - 除了 3D 点之外,是否有 Point 类的等价物?

java - 如何解决android studio中的debug问题

java - 使用服务帐户调用 Java Google Drive Api 时授予无效

android - 如何在用户选择项目时获取 recyclerview 中项目的 ID

java - Android对每个应用程序使用SD卡的存储限制

c# - Xamarin Android C# - 使用 Microsoft.SqlServer.Management

java - 在java中,如何将一个项目导入到另一个项目中而不将它们放在同一个(Eclipse)工作区中?

java - Junit 测试不会使用 Google App Engine 保留实体

java - Android (Java) HttpURLConnection 静默重试 'read' 超时