使用自定义 CursorAdapter 将数据添加到 listView

发布于 2024-12-13 10:51:31 字数 1883 浏览 4 评论 0原文

您好,我想在 ListActivity 中实现以下结果:

  1. 第一项,描述

  2. 第二项,描述

  3. ...

其中名称和描述来自

Cursor c = db.rawQuery(...);

我使用了 SimpleCursorAdapter 并且没有任何问题。然后我决定在显示光标之前对光标中的数据进行一些修改。我想要一个内部计数器,每次光标适配器显示新视图时该计数器都会增加。这就是为什么我决定实现自己的 CustomCursorAdapter 扩展 SimpleCursorAdapter

public class TrainingsListCursorAdapter extends SimpleCursorAdapter{

private Context context;
private int layout;


int list_item_order_number = 1;

public TrainingsListCursorAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to) {


    super(context, layout, c, from, to);

    this.context = context;
    this.layout = layout;



}//contructor


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
    Cursor c = getCursor();

    final LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(layout, parent, false);

    String durationCol      = c.getString(c.getColumnIndex("duration"));

    if(durationCol != null){
        durationCol = minutesForSeconds(durationCol);
    }


    TextView textViewDuration = (TextView) v.findViewById(R.id.duration);
    TextView order_number = (TextView) v.findViewById(R.id.list_item_num);


    if(order_number != null){
        order_number.setText(String.valueOf(list_item_order_number));

    }
    if(textViewDuration != null){
        textViewDuration.setText(durationCol);

    }


    list_item_order_number++;

    return v;

}

我有一个成员变量 _list_item_order_number_ ,每次绘制视图/行时都会加一(通过调用 newView( ) 方法)。这个数字显示在每个视图/列表项中,并且它对应于列表项顺序...

我遇到的问题是,当我旋转屏幕时,这个数字不再对应于列表项。例如,我的第 3 个项目的值为 1 等等...

我想这与 Android 绘制视图的顺序有关。如果我将列表滚动到中间,然后旋转,这些数字都会像这样混合:

  1. 第一项

  2. 第二项

  3. 第三项项目

Hi I want to achieve the following result in my ListActivity:

  1. First item, descritpion

  2. Second item, description

  3. ...

where Name and description come from

Cursor c = db.rawQuery(...);

I used a SimpleCursorAdapter and I had no problems. Then I decided to make some modification to data that I have in cursor before displaying it. I wanted to have a internal counter that would increment every time cursor adapter dislays a new view. That's why I decided to implement my own CustomCursorAdapter extending SimpleCursorAdapter:

public class TrainingsListCursorAdapter extends SimpleCursorAdapter{

private Context context;
private int layout;


int list_item_order_number = 1;

public TrainingsListCursorAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to) {


    super(context, layout, c, from, to);

    this.context = context;
    this.layout = layout;



}//contructor


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
    Cursor c = getCursor();

    final LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(layout, parent, false);

    String durationCol      = c.getString(c.getColumnIndex("duration"));

    if(durationCol != null){
        durationCol = minutesForSeconds(durationCol);
    }


    TextView textViewDuration = (TextView) v.findViewById(R.id.duration);
    TextView order_number = (TextView) v.findViewById(R.id.list_item_num);


    if(order_number != null){
        order_number.setText(String.valueOf(list_item_order_number));

    }
    if(textViewDuration != null){
        textViewDuration.setText(durationCol);

    }


    list_item_order_number++;

    return v;

}

I have a member variable _list_item_order_number_ that is incremented by one every time a view/row is drawn (by calling newView() method). This number is displayed in every view/list item and it corresponds to list item order...

The problem I have is that when I'm rotating the screen this number doesn't correspond to list items anymore. For example my 3rd item has value 1 and so on...

I guess this has something to do with the order Android is drawing the views. If I scroll the list to, let's say, the middle and then rotate, these numbers are all mixed like this:

  1. First Item

  2. Second item

  3. Third item

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文