【问题标题】:Custom SimpleCursorAdapter shows first row when adding a new DB entry添加新数据库条目时,自定义 SimpleCursorAdapter 显示第一行
【发布时间】:2013-05-12 23:46:37
【问题描述】:

我正在使用自定义 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);

    }
}

【问题讨论】:

  • 有什么想法吗?如果问题不清楚或者我需要添加更多代码,请告诉我。

标签: android simplecursoradapter


【解决方案1】:

现在可以使用了: 问题是我标记了错误的观点: 我删除了 newView() 中的所有 setTag(...) 代码,并且只设置了一次标签:

 view.setTag(holder)

在退货之前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多