I am confused about how Android newView and getView in CursorAdapter work. Most of the implementations I have seen so far implement getView instead, however many online sources suggest that that is NOT the way to go, for example here.
My problem boils down to the following bits of code
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Decide if message was sent by Me or Other
View chatRow;
if(cursor.getInt(cursor.getColumnIndexOrThrow(DbChat.KEY_SENDER_ID)) == 0) {
chatRow = LayoutInflater.from(context.getApplicationContext())
.inflate(R.layout.listrow_chat_me,parent,false);
} else {
chatRow = LayoutInflater.from(context.getApplicationContext())
.inflate(R.layout.listrow_chat_other,parent,false);
}
return chatRow;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView chatText = (TextView) view.findViewById(R.id.chat_text);
ImageView imageView = (ImageView) view.findViewById(R.id.chat_img);
chatText.setText(cursor.getString(cursor.getColumnIndexOrThrow(DbChat.KEY_TEXT)));
if(cursor.getInt(cursor.getColumnIndexOrThrow(DbChat.KEY_SENDER_ID)) == 0) {
imageView.setImageDrawable(ChatActivity.sIconBlue);
} else {
imageView.setImageDrawable(ChatActivity.sIconRed);
}
Note that newView sets the layout (including left or right alignment of the image) while bindView sets the image (in this case blue or red). Thus, the expected behavior would be that all red squares be on the left and all blue squares on the right (since both color and position query the same ID-check on the cursor). Instead, I get the following layout:
Essentially, my issue is the same as in this question but I did not find any of the suggestions to solve my problem.
Thanks for any explanation to this weird behavior or solution to the problem!