2015-04-18 70 views
0

我對android編程還很陌生。我遇到了一些問題加載圖像(位圖)到一個ListView。它工作完美,直到我有大約5個圖像加載,然後它重新使用我加載的第一個圖像(從緩存假設)。我嘗試了很多解決方案和位圖採樣,但仍然沒有運氣。這裏是我的代碼:Picasso只加載5張圖片直到重新使用緩存中的圖片

@Override 
    public View getView(int position, View view, ViewGroup parent) { 

     if (view == null) { 

      view = getLayoutInflater().inflate(R.layout.listview_item, parent, false); 

      Contact currentContact = Contacts.get(position); 
      TextView name = (TextView) view.findViewById(R.id.contactName); 

      name.setText(currentContact.getName()); 
      TextView phone = (TextView) view.findViewById(R.id.phoneNumber); 
      phone.setText(currentContact.getPhone()); 
      TextView email = (TextView) view.findViewById(R.id.email); 
      email.setText(currentContact.getEmail()); 
      TextView address = (TextView) view.findViewById(R.id.cAddress); 
      address.setText(currentContact.getAddress()); 

      Context mContext = getApplicationContext(); 

      File file = mContext.getDir("imageDir", Context.MODE_PRIVATE); 

      String path = "file:" + file.getPath() + "/" + currentContact.getImage() + ".png"; 

      ImageView contactListImage = (ImageView) view.findViewById(R.id.contactListImage); 

       Picasso.with(getApplicationContext())  
         .load(path) 
         .centerInside() 
         .fit() 
         .error(R.drawable.user_2) 
         .into(contactListImage) 
     } 
     return view; 
    } 

我曾嘗試使用

recycleMemoryCache() 

謝謝你前進!

回答

0

您沒有正確使用回收視圖。

view參數你有沒有爲null,這意味着你必須誇大一個新的,或不,這意味着你應該再利用它 - 分配新值的所有字段等。

您只需只是你的看法通脹後,移動上述return view;閉合花括號:

if (view == null) { 
    view = getLayoutInflater().inflate(R.layout.listview_item, parent, false); 
} 

順便說一句,我想看看了viewholder模式 - 或者只是開始使用RecyclerView,作爲這強制我t(並且是Android上的列表未來的方式)。

+0

哇,不能相信解決方案如此簡單。現在奇妙地工作。謝謝! – Sabin