3

I have a ListView which displays a list of items. When I click on an item, I mark the item as striked in my database table. Then, I update the list using a SimpleCursorAdapter, setViewBinder, setViewValue.

I check if the striked column is set for a corresponding item, and then I update the TextView to strike the item.

The code is below

Cursor c = db.fetchAllNotes(id);
    startManagingCursor(c);

    String[] from = new String[] { DatabaseHandler.KEY_LIST };
    int[] to = new int[] { R.id.text1 };

    SimpleCursorAdapter notes =
        new SimpleCursorAdapter(this, R.layout.task_row, c, from, to);

    notes.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            // TODO Auto-generated method stub      
            text = (TextView) view.findViewById (R.id.text1);
            System.out.println("Item is "+ text.getText().toString());
            System.out.println("Item from cursor is " + cursor.getString(2));
            System.out.println("Striked value is " + Integer.parseInt(cursor.getString(3)));
            if(Integer.parseInt(cursor.getString(3)) == 1)
                text.setPaintFlags(textViewItem.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            return false;
        }
    });
    setListAdapter(notes);

What am I doing wrong?

mystified
  • 33
  • 3

2 Answers2

5

I think it's because views are reused in ListView. Try to reset paint flags when view is not striked:

 if(Integer.parseInt(cursor.getString(3)) == 1){
     textViewItem.setPaintFlags(textViewItem.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
 } else {
     //resed paint flags
     textViewItem.setPaintFlags(textViewItem.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
 }

Edit: I am not sure if my explatation is corect. When You have 10 items on ListView and only 5 of them are visible android creates(inflantes) only 5 views. When you scroll list one new item will be visible but olso one will disapear so there will be one unused view. Android wil take that unused view, fill it with new data and use it for newly appeared item. I found some more info here.

Leszek
  • 6,568
  • 3
  • 42
  • 53
  • That seemed to do the trick although I would love to know why. When you say views are reused in ListView, what does happen? It's fine if you can't elaborate, I will google it from here on. Thanks a lot. – mystified Jul 24 '12 at 16:24
  • I am glad you want to know more:) I added some explanation to my answer. – Leszek Jul 24 '12 at 16:37
0

you need different types of rows in your list(two types, striked and non striked), here is the best explaination i found for myself

https://stackoverflow.com/a/4778062/579646

Community
  • 1
  • 1
max4ever
  • 11,909
  • 13
  • 77
  • 115