2012-08-29 71 views
-1

我开发了一个列表视图,列表视图显示图像和文本.1必须下载图像和文本形式的网络服务,然后必须显示,因为它需要更多的时间,所以我们认为第一次绑定文本在列表视图和使用AsyncTask,一旦图像下载图像将在活动的背景中显示在列表视图中。但我无法做到这一点,我已经做了一些编码,它下载所有图像,然后绑定图像和文本(在这种情况下我们需要下载image.So后前startDownload图像和2月1日两次绑定列表视图,如果任何有一些想法,请给我建议。文本和图像的自定义列表视图

代码

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

} 
+0

你要做的惰性加载... –

+0

感谢穿心莲内酯,u能告诉我在深怎么办这... – Dilip

+0

我认为用户已经在他的评论中提供了他的答案中的链接。去那个链接并且检查Fedor的答案。这是最好的策略和非常简单的实施。 –

回答

0

试试这个example或使用任何适配器这样

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

确定将在我的上下文中工作........首先阅读我的问题,我必须在活动的背景下从服务器下载图像,并且只要图像下载必须在listview中进行绑定...因此请正确阅读我的问题.. – Dilip

+0

此链接将帮助你http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-i-f-in-listview –

+0

谢谢Rajesh,Deepak已经编辑了你的答案对我来说是错误的,所以我不能为这个答案投票。你的答案非常好,再次感谢你。 – Dilip

相关问题