2012-11-26 118 views
0

我想顯示圖像從URL下載並在列表視圖中顯示它。 這是我的源代碼。輸出有一個空白點..這就是全部......無法在列表視圖上顯示圖像

這是我從中調用的Java。

ABC s = ABC.getSingletonObject(); 
String[][] full_data = s.getString(); 

private ListView listView1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity2); 

    //second one is always the type of data... 
    Weather weather_data[] = new Weather[] 
    {  // full_data[1][0] contains the URL String.. 
     new Weather(full_data[1][0],full_data[1][2],full_data[1][3]), 
     new Weather(full_data[2][0],full_data[2][2],full_data[2][3]), 
    }; 

    WeatherAdapter adapter = new WeatherAdapter(this,R.layout.listview_header_row,          weather_data); 
    listView1 = (ListView)findViewById(R.id.listView1); 
    View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null); 
    listView1.addHeaderView(header); 
    listView1.setAdapter(adapter); 
} 

這是我的適配器..

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.imageView1); 
     holder.txtTitle = (TextView)row.findViewById(R.id.txt1); 
     holder.txtRating=(TextView)row.findViewById(R.id.rtxt1); 

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

    Weather weather = data[position]; 
    holder.txtTitle.setText(weather.wtitle); 
    holder.imgIcon.setImageDrawable(weather.wicon); 
    holder.txtRating.setText(weather.wrating); 
    //holder.imgIcon.setImageResource(R.drawable.ic_launcher); 
    //holder.imgIcon.setImageBitmap(weather.wicon); 
    return row; 
    } 

    static class WeatherHolder 
    { 
    //Bitmap imgIcon; 
    ImageView imgIcon; 
    TextView txtTitle; 
    TextView txtRating; 
    } 
} 

這是其他的java文件

public class Weather { 

    public Drawable publicdraw; 
    public Drawable wicon; 
    public String wtitle; 
    public String wrating; 
    public Weather(){ 
    super(); 
    } 

    public Weather(String icon, String title,String rating) { 
     super(); 
     LoadImageFromWebOperations(icon); 
     this.wtitle = title; 
     this.wrating=rating; 
     this.wicon=publicdraw; 
    } 

    public void LoadImageFromWebOperations(String url_string) { 
     try { 
      grabImage(url_string); 

     } catch (Exception e) { 

     } 
    } 

    public void grabImage(String url) { 
     new GrabURL1().execute(url); 
    } 


    private class GrabURL1 extends AsyncTask<String, Void, Void> { 

    public Drawable d; 
    InputStream is; 

    protected Void doInBackground(String... urls) { 

      try { 
       is = (InputStream) new URL(urls[0]).getContent(); 
       d = Drawable.createFromStream(is, "src name"); 
      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      return null; 

    } 

     protected void onPostExecute(Void unused) { 
      publicdraw=d; 

     } 
    } 
} 
+0

爲什麼不在onPostExecute之後將下載的圖像設置爲imageview? – e7fendy

+0

仍然沒有工作!實際上嘗試了一切......現在我只是在這裏粘貼了一個版本...... –

+0

即使我想這樣做......我會失去以前的瞬間權利? ...因爲我正在使用「this.wicon」...在PostExecte中,我將如何獲得之前的上下文... –

回答

0

我建議你去通過這個tutorial。直截了當,不難,非常有效。我用它來加載列表視圖中的圖像。
如果您遇到困難,請詢問。

+0

我經歷了幾乎所有可能的代碼。 但我有同樣的問題... –

+0

@JS_VIPER,你可以發佈圖像的一些鏈接,只是爲了檢查。 –

0

在將圖像分配到列表之前,請嘗試添加以下條件。

if (holder.imgIcon != null && position < weather_data.size()) { 
     Weather weather = data[position]; 
     holder.imgIcon.setImageDrawable(weather.wicon); 
} 

我有同樣的問題,這個技巧對我來說就像一個魅力工作。

謝謝。