【问题标题】:load image to custom ArrayAdapter将图像加载到自定义 ArrayAdapter
【发布时间】:2015-06-25 01:25:00
【问题描述】:

我使用 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 并链接图像包含在那里,但在此示例中,我使用来自谷歌开发人员的图像

这里还有数组适配器使用的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");

【问题讨论】:

  • 图像不会出现在设备中,只是文本
  • 你应该把这个添加到问题中。
  • 好的,已经添加xml了
  • 我的意思是你应该描述你在问题中遇到的失败/错误。
  • 您有INTERNET 权限吗?此外,还有一些库,例如 PicassoGlide

标签: java android


【解决方案1】:

在 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

【讨论】:

    【解决方案2】:

    我发现您的代码中可以改进的地方很少:
    1。在您的自定义适配器中,您应该使用ViewHolder 来提高您的ListView 性能,因为这些行:convertView.findViewById() 成本太高。
    2。您使用AsyncTask 将您的图片从Internet 加载到GetView()。没关系,但是每当您滚动 ListView 时,您的 GetView() 就会被调用,然后您会启动另一个 AsyncTask。不能保证哪个Task 会先完成,所以您在ListView 中的图像位置可能是错误的(现在您在每一行中加载相同的图像,这不会发生,但将来会发生)。
    因此,我的建议是:在您的适配器中使用ViewHolder,使用一些第三方Lib 来有效地加载图像,例如Universal ImageLoader

    【讨论】:

    • 真的很有效,按你说的做,真的加载很慢 :),现在将寻找 libs ty 的提示:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    相关资源
    最近更新 更多