2012-05-24 65 views
0

我已經修改the code in this question,根據答案那裏,以加載上一個ListView聯繫人的圖片。我得到Photo_id,並使用它來獲取聯繫人的位圖,使用loadContactPhoto(ContentResolver cr,long id)。問題是沒有ImageView獲取新圖像,儘管照片ID始終不同。我嘗試使用Contact._ID,但仍然只有兩個聯繫人的ImageView獲得了聯繫人圖片,並且他們都是錯誤的。我已經評論了我在下面添加的新行。加載聯繫人的圖片到的ListView(部分2)

下面是編輯後的代碼:

ContactStock:

public class ContactStock { 

private String name; 
private String number; 
private Bitmap picture; 


public ContactStock(String name, String number) { 
    this.name = name; 
    this.number = number; 
} 

public ContactStock(String name, String number, Bitmap photo) { 
    this.name = name; 
    this.number = number; 
    this.picture = photo; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public void setNumber(String number) { 
    this.number = number; 
} 

public String getName() { 
    return this.name; 
} 

public String getNumber() { 
    return this.number; 
} 

public void setPicture(Bitmap picture) { // NEW METHOD 
    this.picture = picture; 
} 

public Bitmap getPicture() { // NEW METHOD 
    return picture; 
} 
} 

addlistfromcontact:

public class addlistfromcontact extends Activity { 
private ListView lst; 
private List<ContactStock> contactstock; 
private Cursor mCursor; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tab_contact_list); 
    lst = (ListView) findViewById(R.id.tab_contact_list); 
    contactstock = new ArrayList<ContactStock>(); 

    mCursor = managedQuery(ContactsContract.Data.CONTENT_URI, null, 
      Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", null, 
      ContactsContract.Data.DISPLAY_NAME + " ASC"); 

    int number = mCursor.getColumnIndex(Phone.NUMBER); 
    int name = mCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME); 
    int id = mCursor.getColumnIndex(Contacts.PHOTO_ID); // NEW LINE 

    while (mCursor.moveToNext()) { 

     String phName = mCursor.getString(name); 
     String phNumber = mCursor.getString(number); 
     long phId = mCursor.getLong(id); // NEW LINE 

     Bitmap phPhoto = loadContactPhoto(getContentResolver(), phId); // NEW LINE 
     Log.d("phId=", phId + ""); 

     contactstock.add(new ContactStock(phName, phNumber, phPhoto)); // NEW LINE EDIT 
    } 
    lst.setAdapter(new ContactListAdapter(addlistfromcontact.this, 
      contactstock)); 
} 

public static Bitmap loadContactPhoto(ContentResolver cr, long id) { // NEW METHOD 
    Uri uri = ContentUris.withAppendedId(
      ContactsContract.Contacts.CONTENT_URI, id); 
    InputStream input = ContactsContract.Contacts 
      .openContactPhotoInputStream(cr, uri); 
    if (input == null) { 
     return null; 
    } 
    return BitmapFactory.decodeStream(input); 
} 

    } 

ContactListAdapter:

public class ContactListAdapter extends ArrayAdapter { 
    private final Activity activity; 
    private final List stocks; 

    public ContactListAdapter(Activity activity, List objects) { 
     super(activity, R.layout.listview_detail_tab_contact_list, objects); 
     this.activity = activity; 
     this.stocks = objects; 
    } 

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

     View rowView = convertView; 
     ContactStockView sv = null; 
     if (rowView == null) { 
      // Get a new instance of the row layout view 
      LayoutInflater inflater = activity.getLayoutInflater(); 
      rowView = inflater.inflate(
        R.layout.listview_detail_tab_contact_list, null); 

      // Hold the view objects in an object, 
      // so they don't need to be re-fetched 
      sv = new ContactStockView(); 
      sv.name = (TextView) rowView.findViewById(R.id.contact_name); 
      sv.number = (TextView) rowView.findViewById(R.id.contact_number); 
      sv.photo = (ImageView) rowView.findViewById(R.id.contact_photo); 

      // Cache the view objects in the tag, 
      // so they can be re-accessed later 
      rowView.setTag(sv); 
     } else { 
      sv = (ContactStockView) rowView.getTag(); 
     } 
     // Transfer the stock data from the data object 
     // to the view objects 
     ContactStock currentStock = (ContactStock) stocks.get(position); 
     sv.name.setText(currentStock.getName()); 
     sv.number.setText(currentStock.getNumber()); 
     sv.photo.setImageBitmap(currentStock.getPicture()); // NEW LINE 

     // TODO Auto-generated method stub 
     return rowView; 
    } 

    protected static class ContactStockView { 
     protected TextView name; 
     protected TextView number; 
     protected ImageView photo; // NEW LINE 
    } 
} 
+0

你怎麼解決這個問題? –

+0

閱讀問題的答案。我已經寫了一個自己的所有細節和例子,你需要 –

+0

Okey謝謝兄弟:) –

回答

1

有你的合作兩個問題德。第一個很容易克服,其他人需要更多的工作。

讓我們從簡單的開始: 方法ContactsContract.Contacts.openContactPhotoInputStream(cr, uri)需要一個聯繫人uri而不是照片uri。這是您撥打ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id)的ID必須是聯繫人ID。

而且你遍歷結果集的時候產生很多Bitmap對象。不要這樣做。雖然這可能一開始就有效,但是當列表變長時,它可能會與OutOfMemory錯誤一起崩潰。嘗試儘可能少地創建對象Bitmap。那就是:僅限那些可見的行。滾動列表視圖時,您必須回收現有位圖。

0

因爲我有很多的麻煩搞清楚如何有效地加載所有聯繫人的照片,而無需任何用戶界面凍結,在加載時,我第一次開始研究Android的圖像錯誤,我強烈建議任何人在看我的問題走好看看here

就是這麼教你如何處理所有等待處理數據,並加載圖像seamingly,有效地不帶任何「打嗝」,而滾動列表一個示例應用程序。這也是一個讓您開始使用Android的好項目!

具體來說,對於我的問題代碼的聯繫圖片部分,API提供了兩種簡單的方法,可以找到here,返回聯繫人的高分辨率圖片或縮略圖的InputStream。你所要做的就是使用BitmapFactory.decodeByteArray()對它進行解碼。記住加載你需要在你的應用中使用的圖片的大小(上面鏈接中描述的東西),這樣你就不會得到OutOfMemoryError!

爲了進一步提高代碼,您也可以替換一個自定義光標適配器一個ArrayAdapter,只加載你需要當場列表視圖的線條。

相關問題