I display the information using Listview. However, I have two types of layout file depends on the information_type.
If information_type is 1, I will use one layout file.And if information_type is 2, I will use another layout file.When the ListView loaded, it will automatically go to bottom.
For example at the beginning, I have 40 list items.When the ListView is loaded, for the bottom 4-5 items, it displays using correct layout file.
However, when I scroll the ListView upper and then scroll down, the bottom 4-5 items changes their layout file.
Every time I scroll up and down, the list items(layout file) may changes usually.
Here is my code.
public class CustomCursorAdapter extends SimpleCursorAdapter{
private int layout;
public CustomCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
ViewHolder holder = new ViewHolder();
View v;
final LayoutInflater inflater = LayoutInflater.from(context);
int col_type = cursor.getColumnIndex(KEY_MESSAGE_TYPE);
String type = cursor.getString(col_type);
Log.d(TAG, "in newView: type is:" + type);
if (type.equalsIgnoreCase("1")){
Log.d(TAG, "in newView: receive message");
v = inflater.inflate(R.layout.sub_chat_view_in, parent, false);
holder.tv_title = (TextView)v.findViewById(R.id.titleIn);
holder.tv_message = (TextView)v.findViewById(R.id.messageIn);
holder.tv_time = (TextView)v.findViewById(R.id.timeIn);
}else{
Log.d(TAG, "in newView: send message");
v = inflater.inflate(R.layout.sub_chat_view_out, parent, false);
holder.tv_title = (TextView)v.findViewById(R.id.titleOut);
holder.tv_message = (TextView)v.findViewById(R.id.messageOut);
holder.tv_time = (TextView)v.findViewById(R.id.timeOut);
}
v.setTag(holder);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c){
ViewHolder holder = (ViewHolder) v.getTag();
int col_owner = c.getColumnIndex(KEY_OWNER_NAME);
int col_target = c.getColumnIndex(KEY_TARGET_NAME);
int col_type = c.getColumnIndex(KEY_MESSAGE_TYPE);
int col_message = c.getColumnIndex(KEY_MESSAGE_CONTENT);
int col_time = c.getColumnIndex(KEY_TIME);
String owner = c.getString(col_owner);
String target = c.getString(col_target);
String type = c.getString(col_type);
String message = c.getString(col_message);
String time = c.getString(col_time);
if(type.equalsIgnoreCase("1")){
holder.tv_title.setText("From " + target);
}else{
holder.tv_title.setText("To " + target);
}
holder.tv_message.setText(message);
holder.tv_time.setText(time);
}
class ViewHolder {
TextView tv_title;
TextView tv_message;
TextView tv_time;
}
}
Try to override next methods:
public int getViewTypeCount() and public int getItemViewType(int position)