0

i tring load images and texts to a ListView using a arrayAdapter custom but the load image fail

here are my arrayAdapter

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

here a ImageTask image

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

and here the call from 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);

the arrayOfPost is coming of a parse jonson to a ArrayList and links images is contained there, but in this example i using images from google developer

and the xml used by array adapter here

<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>

any know which are my wrong and how i should to do for load images? i dont right if is the way corret for load the image in the specific ViewImage //this lines are in fist class posted

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

2 Answers2

2

Steps to load an image in an AsyncTask

Step 1:

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

Step 2:

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

Step 3:

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

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

Reference:Android : Loading an image from the Web with Asynctask

Community
  • 1
  • 1
ganeshvjy
  • 399
  • 1
  • 12
1

I see there're few things which can be improved in your code:
1. in your custom adapter you should use ViewHolder to improve your ListView performance because those lines: convertView.findViewById() cost too much.
2. You use AsyncTask to load your Image from the Internet in GetView(). It is ok but whenever you scroll your ListView, your GetView() is called and you start another AsyncTask. There's no guarantee that which Task will finished first, so your image position in ListView maybe wrong (now you load same image in every row, it's not happen, but in the future it will).
Therefore, my suggestion is: use ViewHolder in your adapter, use some 3rd party Lib to load image efficiently such as Universal ImageLoader

Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86