Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame^] | 1 | |
| 2 | package com.android.ex.chips; |
| 3 | |
| 4 | import com.android.ex.chips.BaseRecipientAdapter.EmailQuery; |
| 5 | |
| 6 | import android.content.Context; |
| 7 | import android.database.Cursor; |
| 8 | import android.provider.ContactsContract.CommonDataKinds.Email; |
| 9 | import android.view.LayoutInflater; |
| 10 | import android.view.View; |
| 11 | import android.view.ViewGroup; |
| 12 | import android.widget.CursorAdapter; |
| 13 | import android.widget.TextView; |
| 14 | |
| 15 | public class RecipientAlternatesAdapter extends CursorAdapter { |
| 16 | private final LayoutInflater mLayoutInflater; |
| 17 | |
| 18 | private final int mLayoutId; |
| 19 | |
| 20 | private final int mSelectedLayoutId; |
| 21 | |
| 22 | private final long mCurrentId; |
| 23 | |
| 24 | public RecipientAlternatesAdapter(Context context, long contactId, long currentId, int viewId, |
| 25 | int selectedViewId) { |
| 26 | super(context, context.getContentResolver().query(Email.CONTENT_URI, EmailQuery.PROJECTION, |
| 27 | Email.CONTACT_ID + " =?", new String[] { |
| 28 | String.valueOf(contactId) |
| 29 | }, null), 0); |
| 30 | mLayoutInflater = LayoutInflater.from(context); |
| 31 | mLayoutId = viewId; |
| 32 | mSelectedLayoutId = selectedViewId; |
| 33 | mCurrentId = currentId; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public long getItemId(int position) { |
| 38 | Cursor c = getCursor(); |
| 39 | c.moveToPosition(position); |
| 40 | return c.getLong(EmailQuery.DATA_ID); |
| 41 | } |
| 42 | |
| 43 | public RecipientEntry getRecipientEntry(int position) { |
| 44 | Cursor c = getCursor(); |
| 45 | c.moveToPosition(position); |
| 46 | return RecipientEntry.constructTopLevelEntry(c.getString(EmailQuery.NAME), c |
| 47 | .getString(EmailQuery.ADDRESS), c.getLong(EmailQuery.CONTACT_ID), c |
| 48 | .getLong(EmailQuery.DATA_ID), c.getString(EmailQuery.PHOTO_THUMBNAIL_URI)); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public View getView(int position, View convertView, ViewGroup parent) { |
| 53 | Cursor c = getCursor(); |
| 54 | c.moveToPosition(position); |
| 55 | if (convertView == null) { |
| 56 | convertView = newView(c.getLong(EmailQuery.DATA_ID) == mCurrentId); |
| 57 | } |
| 58 | |
| 59 | bindView(convertView, convertView.getContext(), getCursor()); |
| 60 | return convertView; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void bindView(View view, Context context, Cursor cursor) { |
| 65 | TextView destination = (TextView) view.findViewById(android.R.id.text1); |
| 66 | destination.setText(cursor.getString(EmailQuery.ADDRESS)); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
| 71 | return newView(false); |
| 72 | } |
| 73 | |
| 74 | private View newView(boolean isSelected) { |
| 75 | return isSelected ? mLayoutInflater.inflate(mSelectedLayoutId, null) : mLayoutInflater |
| 76 | .inflate(mLayoutId, null); |
| 77 | } |
| 78 | } |