HI all,im trying a develop a sms app from scratch(smsdroid as my reference) . Basically i have Viewpager which shows the conversation list in a a fragment. The fragment has a listview in it. The list view adapter has two different layouts to show sender's and receivers separately. My listview adapter extends Resource cursor adapter.Everything is working well when the app loads.Now in to the problem the image of the receiver is get replaced by the image of the sender on scrolling the listview. Can some one suggest me a way to overcome this problem?
After scrolling the list looks like this
I have read about listview view recyling and all but to be frank i did not understand it completly.
This is my message adapter for the listview
public class MessageAdapter extends ResourceCursorAdapter {
ViewHolder holder;
private static final String WHERE = "("
+ Message.PROJECTION_JOIN[Message.INDEX_TYPE] + " != "
+ Message.SMS_DRAFT + " OR "
+ Message.PROJECTION_JOIN[Message.INDEX_TYPE] + " IS NULL)";
/** WHERE clause for drafts. */
private static final String WHERE_DRAFT = "("
+ Message.PROJECTION_SMS[Message.INDEX_THREADID] + " = ? AND "
+ Message.PROJECTION_SMS[Message.INDEX_TYPE] + " = "
+ Message.SMS_DRAFT + ")";
// + " OR " + type + " = " + Message.SMS_PENDING;
String sssssss;
Contact globalcontact;
private int mLayout;
Uri uri;
public static AnimationDrawable AniFrame;
public static boolean enableHangoutAnimation=false;
public MessageAdapter(Activity _context, Uri uri) {
super(_context, R.layout.testmessage_classic_received, getCursor(
_context.getContentResolver(), uri), true);
this.defaultContactAvatar = _context.getResources().getDrawable(
R.drawable.default_avatar);
this.ownerAvatar = _context.getResources().getDrawable(
R.drawable.bubble_orange_right);
this.backgroundDrawableIn = PreferencesActivity.getBubblesIn(_context);
this.backgroundDrawableOut = PreferencesActivity
.getBubblesOut(_context);
this.textSize = PreferencesActivity.getTextsize(_context);
this.textColor = PreferencesActivity.getTextcolor(_context);
this.convertNCR = PreferencesActivity.decodeDecimalNCR(_context);
context = _context;
this.uri = uri;
if (uri == null || uri.getLastPathSegment() == null) {
this.threadId = -1;
} else {
this.threadId = Integer.parseInt(uri.getLastPathSegment());
}
Conversation conv = Conversation.getConversation(context,
this.threadId, false);
if (conv == null) {
this.address = null;
this.name = null;
this.displayName = null;
} else {
contact = conv.getContact();
this.address = contact.getNumber();
this.name = contact.getName();
this.displayName = contact.getDisplayName();
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowType = getItemViewType(cursor.getPosition());
if (rowType == 0) {
return mInflater.inflate(R.layout.testmessage_classic_sent, parent,
false);
} else if (rowType == 1) {
return mInflater.inflate(R.layout.testmessage_classic_received,
parent, false);
} else {
return null;
}
}
@Override
public int getItemViewType(int position) {
Cursor c = (Cursor) getItem(position);
Message m = Message.getMessage(context, c);
switch (m.getType()) {
case Message.MMS_IN: // 128
return 1;
case Message.MMS_OUT: // 132
return 0;
case Message.SMS_IN: // 2
return 1;
case Message.SMS_OUT: // 1
return 0;
default:
return 0;
}
}
public void setImageView(ImageView contactPhoto) {
mProjection = new String[] { Profile._ID, Profile.DISPLAY_NAME_PRIMARY,
Profile.LOOKUP_KEY, Profile.PHOTO_URI };
mProfileCursor = context.getContentResolver().query(
Profile.CONTENT_URI, mProjection, null, null, null);
if (mProfileCursor.moveToFirst()) {
do {
sssssss = mProfileCursor.getString(mProfileCursor
.getColumnIndex(Profile.PHOTO_URI));
if (sssssss != null) {
Uri photoUri = Uri.parse(sssssss);
contactPhoto.setImageURI(photoUri);
} else {
contactPhoto.setImageResource(R.drawable.ic_launcher);
}
} while (mProfileCursor.moveToNext());
}
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
/**
* checking starts here
*
*/
holder = (ViewHolder) view.getTag();
if (holder == null) {
holder = new ViewHolder();
holder.tvBody = (TextView) view.findViewById(R.id.textBody);
holder.tvDate = (TextView) view.findViewById(R.id.textDate);
// holder.vRead= (View) view.findViewById(R.id.read);
Utilities.setCustomFont(context, holder.tvDate);
Utilities.setCustomFont(context, holder.tvBody);
// holder.tvDate = ( TextView ) view.findViewById( R.id.date );
holder.ivPhoto = (QuickContactBadge) view
.findViewById(R.id.imageContactPicture);
holder.btnDownload = (Button) view
.findViewById(R.id.downloadButton);
holder.media = (ImageView) view.findViewById(R.id.media);
holder.ellipse = (ImageView) view.findViewById(R.id.ellipsis);
AniFrame = (AnimationDrawable) holder.ellipse.getBackground();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if (MainActivity.showContactPhoto) {
holder.ivPhoto.setImageDrawable(contact.getAvatar(this.context,
this.defaultContactAvatar));
holder.ivPhoto.setVisibility(View.VISIBLE);
holder.ivPhoto.setOnClickListener(WRAPPER.getQuickContact(
context, holder.ivPhoto,
contact.getLookUpUri(context.getContentResolver()), 2,
null));
if (rowType == 0) {
setImageView(holder.ivPhoto);
} else if (rowType == 1) {
holder.ivPhoto.setImageDrawable(contact.getAvatar(
this.context, this.defaultContactAvatar));
}
} else {
holder.ivPhoto.setVisibility(View.GONE);
}
}
/** View holder. */
public static class ViewHolder {
public static ImageView ellipse;
TextView tvBody;
TextView tvPerson;
TextView tvDate;
ImageView media;
// View vRead;
public View vPending;
public View vLayout;
public ImageView ivInOut;
public Button btnDownload;
public Button btnImport;
QuickContactBadge ivPhoto;
}
/**
* Get the {@link Cursor}.
*
* @param cr
* {@link ContentResolver}
* @param u
* {@link Uri}
* @return {@link Cursor}
*/
private static Cursor getCursor(final ContentResolver cr, final Uri u) {
Log.d(TAG, "getCursor(" + u + ")");
final Cursor[] c = new Cursor[] { null, null };
int tid = -1;
try {
tid = Integer.parseInt(u.getLastPathSegment());
} catch (Exception e) {
Log.e(TAG, "error parsing uri: " + u, e);
}
try {
Log.d(TAG, "where: " + WHERE);
c[0] = cr.query(u, Message.PROJECTION_JOIN, WHERE, null, null);
} catch (NullPointerException e) {
Log.e(TAG, "error query: " + u + " / " + WHERE, e);
c[0] = null;
} catch (SQLiteException e) {
Log.e(TAG, "error getting messages", e);
}
final String[] sel = new String[] { String.valueOf(tid) };
try {
Log.d(TAG, "where: " + WHERE_DRAFT + " / sel: " + sel);
c[1] = cr.query(Uri.parse("content://sms/"),
Message.PROJECTION_SMS, WHERE_DRAFT, sel, Message.SORT_USD);
} catch (NullPointerException e) {
Log.e(TAG, "error query: " + u + " / " + WHERE_DRAFT + " sel: "
+ sel, e);
c[1] = null;
} catch (SQLiteException e) {
Log.e(TAG, "error getting drafts", e);
}
if (c[1] == null || c[1].getCount() == 0) {
return c[0];
}
if (c[0] == null || c[0].getCount() == 0) {
return c[1];
}
return new MergeCursor(c);
}
}
Can some one explain what is the root cause of this problem and a way to solve it