gpt4 book ai didi

android - 如何显示 ListView 的行号

转载 作者:太空宇宙 更新时间:2023-11-03 13:05:37 26 4
gpt4 key购买 nike

我有一个 ListView ,它包含三个 TextView (id、name、score)。我正在显示数据库中的名称和分数,我想将 id 显示为递增的数字(1、2、3、4。)。我怎样才能做到这一点?

我也可以显示 id 表单数据库,但问题是我显示的是姓名和分数,分数是按降序排列的(按 DESC 排序)。

最佳答案

感谢您的回答。它确实解决了我面临的类似问题。这是我所做的详细示例:

  1. 我有一个 ListView,其中每一行都是一个 RelativeLayout。 RelativeLayout 包含一个 ID 为“serial_no”的 TextView,它应该包含我要显示的行号
  2. 我扩展了 SimpleCursorAdapter 如下:

    public class CustomCursoAdapter extends SimpleCursorAdapter{

    public CustomCursoAdapter(Context context, int layout, Cursor c,
    String[] from, int[] to) {
    super(context, layout, c, from, to);
    // TODO Auto-generated constructor stub
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor)
    {
    int temp =cursor.getPosition()+1;
    RelativeLayout rl = (RelativeLayout) view;
    TextView tv = (TextView) rl.findViewById(R.id.serial_no);
    tv.setText(""+temp);
    super.bindView(view, context, cursor);
    }
    }
  3. 在我的 Activity 类中,我现在调用 CustomCursorAdapter,而不是调用 SimpleCursorAdapter。并且有效!

关于android - 如何显示 ListView 的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5266289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com