我正在使用自定义SimpleCursorAdapter来填充listView。当我在表中添加新条目时,列表视图将第一个DB条目添加到ListView的末尾。 为什么要插入第一行而不是最后一行?
我想这是一个更新问题,因为重新启动应用时,列表视图会正确显示所有行。
我正在使用特殊光标适配器来交替每行的颜色。如果我只使用SimpleCursorAdapter,一切正常。
public class SpecialAdapter extends SimpleCursorAdapter {
private static class ViewHolder {
TextView mTitle;
TextView mDate;
String mDateString;
TextView mCountDown;
String mCountDownString;
}
private int[] colors = new int[] { 0xAAf6ffc8, 0xAA538d00 };
private LayoutInflater mInflater;
private String[] data;
public SpecialAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
mInflater = LayoutInflater.from(context);
}
@Override
public void setViewText(TextView v, String text) {
if(v.getId() == R.id.dateTimeOrLocationID) {
long ms = Long.parseLong(text);
Date date = new Date(ms);
String formatedText = date.toString();
//do format
super.setViewText(v, formatedText);
} else {
super.setViewText(v, text);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.row, parent, false);
int colorPos = cursor.getPosition() % colors.length;
view.setBackgroundColor(colors[colorPos]);
holder.mTitle = (TextView) view.findViewById(R.id.titleID);
int col = cursor.getColumnIndex(DBAdapter.KEY_TITLE);
holder.mTitle.setText(cursor.getString(col));
holder.mTitle.setTag(holder);
col = cursor.getColumnIndex(DBAdapter.KEY_DATE);
Date date = new Date(cursor.getLong(col));
holder.mDate = (TextView) view.findViewById(R.id.dateTimeOrLocationID);
holder.mDate.setText(date.toString());
holder.mDateString = date.toString();
holder.mDate.setTag(holder);
Calendar cal = Calendar.getInstance();
long diff = cal.getTimeInMillis() - date.getTime();
Date diffDate = new Date(diff);
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
holder.mCountDown = (TextView) view.findViewById(R.id.countdownID);
holder.mCountDown.setText(timeFormat.format(diffDate));
holder.mCountDownString = timeFormat.format(diffDate);
holder.mCountDown.setTag(holder);
return view;
}
@Override
public void bindView(View convertView, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder)convertView.getTag();
if(holder != null) {
int col = cursor.getColumnIndex(DBAdapter.KEY_TITLE);
holder.mTitle.setText(cursor.getString(col));
int colorPos = cursor.getPosition() % colors.length;
convertView.setBackgroundColor(colors[colorPos]);
holder.mDate.setText(holder.mDateString);
holder.mCountDown.setText(holder.mCountDownString);
}
}
答案 0 :(得分:0)
现在有效: 问题是我标记了错误的观点: 我删除了newView()中的所有setTag(...)代码,只设置了一次标记:
view.setTag(holder)
返回之前。