1

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.

image 1 image 2

What is wrong??? Thanks!!

helldron
  • 77
  • 1
  • 8

2 Answers2

1

You shoud use one view for left and right messages and change gravity in bindView when data is bindet to display.

Now You have method newView who creates view but when you scrolling view is reused (sometimes have orange, sometimes have green color)

Remove newView metod an make all things in bindView (use one view) - will be more simple and clean ;-)

I make that on my message list - change gravity of LinearLayout

But if You must use 2 view use getView to inflate view - but then You lost efficient view creation (reuse)

Kenumir
  • 664
  • 8
  • 24
  • i give you +1 for answer but now i can't test your hint cause i have replaced this function with another different. Sorry. – helldron Nov 16 '13 at 16:24
0

add final to your text box or use holder for the textbox

Jow
  • 181
  • 1
  • 5
  • I tried an implementation holder pattern but not works yet. As i know the holder pattern is not necessary inside a CursorAdapter http://stackoverflow.com/questions/4567969/viewholder-pattern-correctly-implemented-in-custom-cursoradapter I know that something it isn't ok, but it's weird... – helldron Sep 21 '13 at 13:13