2011-09-09 58 views
0

您好,我的問題很簡單:imageviews不固定。我在這個鏈接中看到了一個答案:link,我明白答案,但我做不到。所以我的問題是:我如何關閉在我的適配器視圖回收?ImageView繼續在我的列表視圖中移動因爲asynctask

如果你想我可以把代碼一點點:

public class PortfolioAdapter extends ArrayAdapter<PortfolioView>{ 
private ArrayList<PortfolioView> items; 
public PortfolioAdapter(Context context, int textViewResourceId, ArrayList<PortfolioView> items) { 
    super(context, textViewResourceId, items); 
    this.items = items; 
} 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if (v == null) { 
     LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.portfolio_rows, null); 
    } 
    PortfolioView pv = items.get(position); 
    if (pv != null) { 
      TextView ticker = (TextView) v.findViewById(R.id.portfolio_rows_ticker); 
      TextView location = (TextView)v.findViewById(R.id.portfolio_rows_location); 
      TextView country = (TextView)v.findViewById(R.id.portfolio_rows_country); 
      TextView portfolio_value = (TextView)v.findViewById(R.id.portfolio_rows_portfolio_value); 
      TextView yesterday_earnings = (TextView)v.findViewById(R.id.portfolio_rows_yesterday_earnings); 
      TextView shares = (TextView)v.findViewById(R.id.portfolio_rows_shares); 
      TextView last_buy_shares = (TextView)v.findViewById(R.id.portfolio_rows_last_buy_shares); 
      TextView last_buy = (TextView)v.findViewById(R.id.portfolio_rows_last_buy); 
      TextView your_shares_held = (TextView)v.findViewById(R.id.portfolio_rows_your_shares_held); 
      ImageView SmPortrait = (ImageView)v.findViewById(R.id.portfolio_rows_sm_portrait); 
      if (ticker != null) { 
        ticker.setText(pv.getPortfolio_ticker()); 
      } 
      if (location != null) { 
       location.setText(pv.getLocation()); 
      } 
      if (country != null) { 
       country.setText(pv.getCountry()); 
      } 
      if (portfolio_value != null) { 
       DecimalFormat f_portfolio_value = new DecimalFormat(); 
       f_portfolio_value.setMaximumFractionDigits(2); 
       String portfolio_value_format = f_portfolio_value.format(pv.getPortfolio_value()); 

       portfolio_value.setText(portfolio_value_format); 
      } 
      if (yesterday_earnings != null) { 
       DecimalFormat f_yesterday_earnings = new DecimalFormat(); 
       f_yesterday_earnings.setMaximumFractionDigits(2); 
       String yesterday_earnings_format = f_yesterday_earnings.format(pv.getYesterday_earnings()); 

       yesterday_earnings.setText(yesterday_earnings_format); 
      } 
      if (shares != null) { 
       shares.setText(Integer.toString(pv.getShares())); 
      } 
      if (last_buy_shares != null) { 
       last_buy_shares.setText(Integer.toString(pv.getLast_buy_shares())); 
      } 
      if (last_buy != null) { 
       last_buy.setText(pv.getLast_buy()); 
      } 
      if (your_shares_held != null) { 
       your_shares_held.setText(Integer.toString(pv.getYour_shares_held())); 
      } 
      if(SmPortrait != null){ 
       createimage(SmPortrait, pv.getSm_portrait()); 
      } 

    } 
    return v; 
} 
private class CreateImage extends AsyncTask<String, Void, Drawable> { 
    ImageView image; 
    public CreateImage(ImageView img) { 
     image = img; 
     image.invalidate(); 
    } 
    protected void onPreExecute() { 
    } 

    protected Drawable doInBackground(String... urls) { 
     InputStream is; 
     Drawable d = null ; 
     try { 
      is = (InputStream)new URL(urls[0]).getContent(); 
      d = Drawable.createFromStream(is, "Image"); 
      return d; 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return d; 
    } 
    protected void onPostExecute(Drawable d) { 
     image.setBackgroundDrawable(d); 
     image.invalidateDrawable(d); 

    } 
} 
// Catch portrait 
public void createimage(ImageView img, String url){ 
    new CreateImage(img).execute(url); 
} 

}

+0

回收的,便會讓OOM錯誤 – ingsaurabh

+0

所以,我能做些什麼呢? – Tsunaze

+0

顯示一個虛擬的圖像,而所需的圖像不下載,因爲丹尼爾建議 – ingsaurabh

回答

0

的基本思想是,當你設置你的觀點,你要添加的縮略圖作爲佔位符。然後使用Tag屬性用類似數據庫中ID的標記來「標記」視圖 - 這可以用來確定哪一行應該獲取圖像,並且不應該再經歷行移動。

請看Fedor對這個問題的回答:link - 它爲您提供了一個很好的例子。

+0

好吧,但他的圖像來自SD卡,所以圖像下載是完全不同的。 – Tsunaze

+0

嗯,這並不完全不同 - 他仍然下載圖像並將它們存儲在SD卡上。在我的實施中,我並沒有因爲我不允許存儲圖像。 –

+0

所以我需要做的是直接在適配器中傳遞我的字符串URL列表? – Tsunaze

0

設置背景後,您不需要使圖像可繪製無效。刪除

image.invalidateDrawable(d); 

onPostExecute()方法行,看看它是否修復。

0

關於Class ImageView的invalidateDrawable方法是什麼?

我已經找到源核心波紋管:

@Override 
public void invalidateDrawable(Drawable dr) { 
    if (dr == mDrawable) { 
     /* we invalidate the whole view in this case because it's very 
     * hard to know where the drawable actually is. This is made 
     * complicated because of the offsets and transformations that 
     * can be applied. In theory we could get the drawable's bounds 
     * and run them through the transformation and offsets, but this 
     * is probably not worth the effort. 
     */ 
     invalidate(); 
    } else { 
     super.invalidateDrawable(dr); 
    } 
}