How to display line number in list view

I have a list view that has three text views (id, name, score). I am showing the name and rating from the database, and I want to show the identifier as an increasing number (1, 2, 3, 4). How can i do this?

I can also display the identifier database, but the problem is that I am showing the name and rating with the score in descending order (ORDER by DESC).

+3
source share
3 answers

From what I understand, you can directly set the identifier in your view (position + 1). From this point of view, your "ID" is actually a counter.

+1
source

. , . , :

  • ListView, - RelativeLayout. RelativeLayout TextView 'serial_no', ,
  • 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);
        }
    }
    
  • Activity SimpleCursorAdapter CustomCursorAdapter. !

+4

Use this line of code and display in a TextView or add text to the list.

cursor.getPosition()+1;
0
source

All Articles