Arash GM
Arash GM

Reputation: 10395

Row number in ListView

I have a ListView that reads data from sqlite with SimpleCursorAdapter and the table has about 1000 rows but i've filtered my list in my Activity by date, so the filtered cursor contains 2 rows for that special day.Therefor i wanted to add a custom row number(can't use _id) for my list.one soloution that i've tought about,was ViewBinder, here is my code:

adapter.setViewBinder(new ViewBinder() {
    public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {
        if (aColumnIndex == 0) {
            aCursor.moveToFirst();
            if(aCursor.moveToFirst()) {
                TextView textView = (TextView) aView;
                textView.setText("" + WeeklyListRowNumber);
                WeeklyListRowNumber = WeeklyListRowNumber + 1;
            }
            return true;
        }
        return false;
    }
});

I have 11 columns in my List and WeeklyListRowNumber initialized 1 on the top,my problem is my rownumbers turns to 7,8 but it must be 1 , 2.can somebody tells me how can I solve this issue?

Upvotes: 1

Views: 4629

Answers (3)

deepak825
deepak825

Reputation: 432

public View getView(int position, View convertView, ViewGroup parent) {
        View retval = null;

            retval = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.content, null);

            title = (TextView) retval.findViewById(R.id.contactName);
            number = (TextView) retval.findViewById(R.id.contactNumber);
            title.setText(text to display in list);
            number.setText(""+position);//add row number to list //fixed the variable
        }

Upvotes: 1

Arash GM
Arash GM

Reputation: 10395

finaly i've soved my problem with ViewBinder :

adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

        public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {
            if (aColumnIndex == 0) {
                    TextView textView = (TextView) aView;
                    int CursorPos = aCursor.getPosition() + 1;
                    textView.setText(Integer.toString(CursorPos));

                return true;
             }

            return false;
        }});

Upvotes: 1

deepak825
deepak825

Reputation: 432

As You are using the adapter for the list view so u may getting the position variable in getview . Use that position (int) as custom list row number it will start from zero(0).

Set it according as per requirement...

Upvotes: 1

Related Questions