Advertisement
ChocoMan

Untitled

Jun 18th, 2012
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. public class CustomAdapter extends ArrayAdapter<String> {
  2.  
  3.     Cursor c;
  4.     String TAG = "CustomAdapter";
  5.     private Context context = null;
  6.     ArrayList<String> elements = null;
  7.     private ArrayList<String> data = null;
  8.  
  9.     public static String contactName;
  10.     public static int count = 0;
  11.  
  12.     private ArrayList<Boolean> itemChecked = null;
  13.     public static List<String> messages;
  14.     public static List<String> contactID;
  15.  
  16.     String body;
  17.     String phoneNumber;
  18.  
  19.     public CustomAdapter(Context context, int type, ArrayList<String> elements) {
  20.         super(context, type, elements);
  21.  
  22.         data = elements;
  23.         this.elements = elements;
  24.         this.context = context;
  25.     }
  26.  
  27.     // THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
  28.     static class ViewHolder {
  29.         public ImageView photo;
  30.         public TextView contact;
  31.     }
  32.  
  33.     @Override
  34.     public View getView(final int position, View convertView, ViewGroup parent) {
  35.         View rowView = convertView;
  36.         final ViewHolder holder;
  37.  
  38.         if (rowView == null) {
  39.             LayoutInflater inflater = (LayoutInflater) context
  40.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  41.  
  42.             // HERE I AM INFLATING LISTVIEW LAYOUT.
  43.             rowView = inflater.inflate(R.layout.contact_entry, null, false);
  44.             holder = new ViewHolder();
  45.             holder.photo = (ImageView) rowView.findViewById(R.id.iv_contactPic);
  46.             holder.contact = (TextView) rowView
  47.                     .findViewById(R.id.contactEntryText);
  48.             rowView.setTag(holder);
  49.  
  50.             // RETRIEVE LATEST CONTACTS WHO SENT SMS (for visual)
  51.             contactID = new ArrayList<String>();
  52.             contactID = elements;
  53.  
  54.             String folder = "content://sms/inbox/";
  55.             Uri mSmsQueryUri = Uri.parse(folder);
  56.             contactID = new ArrayList<String>();
  57.  
  58.             try {
  59.                 c = context.getContentResolver().query(
  60.                         mSmsQueryUri,
  61.                         new String[] { "_id", "thread_id", "address", "date",
  62.                                 "body" }, null, null, null);
  63.                 if (c == null) {
  64.                     Log.i(TAG, "cursor is null. uri: " + mSmsQueryUri);
  65.                 }
  66.  
  67.                 c.moveToFirst();
  68.                 while (c.moveToNext()) {
  69.                     phoneNumber = c.getString(0);
  70.                     contactID.add(phoneNumber);
  71.                 }
  72.  
  73.             } catch (Exception e) {
  74.                 // Log.e(TAG, e.getMessage());
  75.             } finally {
  76.                 c.close();
  77.             }
  78.  
  79.         } else {
  80.             holder = (ViewHolder) rowView.getTag();
  81.         }
  82.  
  83.         if (holder != null) {
  84.            
  85.             // bind the data to the row views
  86.             holder.contact.setText(data.get(position));
  87.             holder.photo.setImageBitmap(getByteContactPhoto(contactID
  88.                     .get(position)));
  89.  
  90.         // SHOW CONTACT PHOTO IF IT EXISTS. IF NOT, DEFAULT (***NOT WORKING***)
  91.         Long l = Long.parseLong(contactID.get(position));
  92.             contactPhoto = loadContactPhoto(context.getContentResolver(), l);
  93.             if(contactPhoto == null){
  94.                 holder.photo.setImageResource(R.drawable.ic_intel);
  95.             } else{
  96.                 holder.photo.setImageBitmap(contactPhoto);
  97.             }
  98.  
  99.         return rowView;
  100.     } // end if
  101.  
  102.         // GET CONTACT PHOTO
  103.     private static Bitmap loadContactPhoto(ContentResolver cr, long  id) {
  104.     Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
  105.     InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
  106.     if (input == null) {
  107.         return null;
  108.     }
  109.     return BitmapFactory.decodeStream(input);
  110. }
  111.  
  112.    
  113. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement