0

I have developed a listview,listview displaying image and text.1st have to download image and text form web-service then have to display because it takes more time so we thought 1st bind text in listview and use AsyncTaskand as soon as image download image will be shown in the listview in the background of activity.But i'm unable to do that i have done some coding and it download all image and then bind both image and text(in this case we need to bind listview two times 1st before startDownload image and 2nd after download image.So if any has some idea please suggest me.

Code

public class LoadImg extends AsyncTask<String, Void, Bitmap> {
    Context context;
    String img;
    InputStream is = null;
    Bitmap bitmap = null;

    public LoadImg(Context context, String img) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.img = img;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        // TODO Auto-generated method stub
        Bitmap bitmap = downImg();
        System.out
                .println("Value of bitmap====================================="
                        + bitmap);
        return bitmap;
    }

    private Bitmap downImg() {
        // TODO Auto-generated method stub
        Bitmap bitmap = null;
        if (img == null) {
            bitmap = null;
        } else {

            URL url = null;
            try {
                url = new URL(img);

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
            URLConnection connection = null;
            try {

                connection = url.openConnection();

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                is = connection.getInputStream();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            bitmap = BitmapFactory.decodeStream(is);
            System.out.println("TV Image===" + bitmap);
        }

        return bitmap;
    }

}
Dilip
  • 2,271
  • 5
  • 32
  • 52
  • Thanks Andro, Can u tell me in deep how to do this... – Dilip Aug 29 '12 at 11:28
  • I think the user has already provided with the link in his comment for his answer. Go to that link and check for Fedor's answer. It is the best strategy and very easy implementation. – Andro Selva Aug 29 '12 at 11:34

1 Answers1

1

Try this example or use any adapter like this

WeatherAdapter.java

public class WeatherAdapter extends ArrayAdapter<Weather>{

Context context;
int layoutResourceId;   
Weather data[] = null;

public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    WeatherHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new WeatherHolder();
        holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
        holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

        row.setTag(holder);
    }
    else
    {
        holder = (WeatherHolder)row.getTag();
    }

    Weather weather = data[position];
    holder.txtTitle.setText(weather.title);
    holder.imgIcon.setImageResource(weather.icon);

    return row;
}

static class WeatherHolder
{
    ImageView imgIcon;
    TextView txtTitle;
}
}
Deepak Swami
  • 3,838
  • 1
  • 31
  • 46
Rajesh Rajaram
  • 3,271
  • 4
  • 29
  • 48
  • r u sure will this work in my context........First read my question i have to download image from server in background of the activity and as soon as image download have to bind in listview...so read properly my question.. – Dilip Aug 29 '12 at 11:22
  • This link will help you http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview – Rajesh Rajaram Aug 29 '12 at 11:30
  • thanks Rajesh,Deepak has edited your answer its wrong for me,So i can't vote for this answer.Your answer is great thank you so much once again. – Dilip Aug 29 '12 at 11:58
  • @Dilip now you got the solution? – Rajesh Rajaram Aug 29 '12 at 12:02
  • @Yeah but stackoverflow.com/questions/541966/…is usefull not above edited code thanks.. – Dilip Aug 29 '12 at 12:13