2011-09-09 3 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

선반 작업 – ingsaurabh

+0

그래서 내가 대신 무엇을 할 수 OOM 오류를 드릴 것? – Tsunaze

+0

Daniel이 – ingsaurabh

답변

0

기본적인 아이디어는 당신이 자리 표시 자로 썸네일을 추가 할 때 설정보기를 그. 그런 다음 Tag 속성을 사용하여 뷰를 데이터베이스의 ID와 같은 것으로 "표시"하십시오. 그러면 이미지를 가져올 행을 식별하는 데 사용할 수 있으며 행을 더 이상 이동하지 않아야합니다.

이 질문에 대한 답변을 Fedor : link에서 살펴보십시오. 아주 좋은 예를 제공합니다.

+0

좋아,하지만 이미지는 SD 카드에서 가져온 것이므로 이미지 다운 로더는 완전히 다릅니다. – Tsunaze

+0

그게 다른 점은 아닙니다. 그는 여전히 이미지를 다운로드하여 SD 카드에 저장합니다. 내 구현에서는 내가 이미지를 저장할 수 없기 때문에하지 않았다. –

+0

그래서 내가해야 할 일은 어댑터에 직접 문자열 URL 목록을 전달하는 것입니다. – Tsunaze

0

배경을 설정 한 후에 이미지 드로어 블을 무효화 할 필요가 없습니다. onPostExecute() 방법에서

image.invalidateDrawable(d); 

라인을 제거하고 고정되는지.

0

클래스 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); 
    } 
}