2012-08-29 68 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

相關問題