Note that there are some explanatory texts on larger screens.

plurals
  1. POLoad a contact's picture into ListView (part 2)
    primarykey
    data
    text
    <p>I have modified <a href="https://stackoverflow.com/questions/8338038/load-a-contacts-picture-into-a-listview-instead-of-the-default">the code in this question</a> ,according to the answers there, in order to load contact's picture on a ListView. I am getting the Photo_id, and use it to get the Bitmap of the contact, using loadContactPhoto(ContentResolver cr, long id) . The problem is that no ImageView is getting a new image, although the photo id is always different. I tried using the Contact._ID, but still only two contacts' ImageView got a contact picture, and they were both wrong. I have commented the new lines I have added below.</p> <p>Here is the code after the edit:</p> <p>ContactStock:</p> <pre><code>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; } } </code></pre> <p>addlistfromcontact:</p> <pre><code> public class addlistfromcontact extends Activity { private ListView lst; private List&lt;ContactStock&gt; 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&lt;ContactStock&gt;(); 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); } } </code></pre> <p>ContactListAdapter:</p> <pre><code>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 } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload