を使用して情報を表示しますListview
。ただし、information_typeに応じて2種類のレイアウトファイルがあります。
information_typeが1
の場合は1つのレイアウトファイルを使用し、information_typeが2
の場合は別のレイアウトファイルを使用しますListView
。ロードされると、自動的に一番下に移動します。
たとえば、最初は40個のリストアイテムがListView
あります。が読み込まれると、下の4〜5個のアイテムについて、正しいレイアウトファイルを使用して表示されます。
ただし、ListView
上をスクロールしてから下にスクロールすると、下の4〜5個のアイテムでレイアウトファイルが変更されます。
上下にスクロールするたびに、通常、リストアイテム(レイアウトファイル)が変わる場合があります。
これが私のコードです。
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;
}
}