i'm facing with an implementation of a chat layout.
I have some data that come from sqlite DB, a Cursor Adapter and 2 layouts: - one that display data on the right - one that display data on the left
As you can see the layout have only two difference: the backgroound and the orientation (left, right). I will choose the layout programmatically, if the message is mine or from the user with i'm talking.
This are 2 layouts of a List View row.
Layout right:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/roomLisViewtMessageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/speech_bubble_green"
android:layout_margin="5dip"
android:textColor="#FFF"
android:textSize="20sp" /></RelativeLayout>
Layout left
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/roomLisViewtMessageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/speech_bubble_orange"
android:layout_margin="5dip"
android:textColor="#FFF"
android:textSize="20sp" /></RelativeLayout>
As you can see i switch the layout in base of a parameter. Initially everything is ok but, when i start scroll the List View, the layouts are assigned in a wrong way as if the adapter lost something.
public class MyCursorAdapter extends CursorAdapter {
private static final String tag = "ADAPTER";
private LayoutInflater layoutInflater;
private Context mContext;
public MyCursorAdapter (Context context, Cursor c, int flags) {
super(context, c, flags);
mContext = context;
layoutInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
int isMine = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper._IS_MINE));
if(isMine==1) return layoutInflater.inflate(R.layout.room_list_view_left, parent, false);
else return layoutInflater.inflate(R.layout.room_list_view_right, parent, false);
}
/**
* @param view: The view in which the elements we set up here will be displayed.
* @param context: The running context where this ListView adapter will be active.
* @param cursor: The Cursor containing the query results we will display.
*/
@Override
public void bindView(View view, Context context, Cursor cursor) {
Log.v(tag,"position---->"+cursor.getPosition());
int isMine = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.IS_MINE));
String mess = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.MESSAGE));
TextView messText = (TextView) view.findViewById(R.id.myLisViewtMessageText);
if (messText != null){
if(isMine==1){
Log.d(tag,"mess = "+mess+" isMine = "+isMine);
}
else{
Log.d(tag,"mess = "+mess+" isMine = "+isMine);
}
messText.setText(mess);
}
}
So i have a normal behaviur in image 1 and after scroll down and return at the top i see messages like in image 2 and for every scroll the screen change.
What is wrong??? Thanks!!