1

I have annoying problem. I have created simple ArrayAdapter for my ListView.

ArrayAdapter<String> adapter = new ArrayAdapter<>(InsideNotebookActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, userNotes.notes);

Now, in object userNotes (from UserNotes class) I have some boolean values and based on true/false I have to set strike flag on each line.

I did this using onItemClick and it works good:

notes_container.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            TextView tv = (TextView) view.findViewById(android.R.id.text1);
            int flags = tv.getPaintFlags();
            flags &= Paint.STRIKE_THRU_TEXT_FLAG;
            if (flags == 0)
                tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            else
                tv.setPaintFlags(tv.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
        }
    });

So, each time user click on item strike flag is on or off.

But, at first I have to make my initialization based on these boolean field in UserNotes class. So, I did this:

for(int i = 0; i < notes_container.getAdapter().getCount(); i++) {
    View view = notes_container.getAdapter().getView(i, null, null);
    TextView tv = (TextView) view.findViewById(android.R.id.text1);
    if (userNotes.select.get(i).matches("True")) {
            tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        }
        else {
            tv.setPaintFlags(tv.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
        }
}

And, it doesn't work. Well, if I put line like this:

tv.setText("CustomText");

At the beggining of for loop and put some logs it looks like this:

for(int i = 0; i < notes_container.getAdapter().getCount(); i++) {
    View view = notes_container.getAdapter().getView(i, null, null);
    TextView tv = (TextView) view.findViewById(android.R.id.text1);
    Log.d("MY_LOG_1",tv.getText().toString());
    tv.setText("CustomText");
    Log.d("MY_LOG_2",tv.getText().toString());
    if (userNotes.select.get(i).matches("True")) {
            tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        }
        else {
            tv.setPaintFlags(tv.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
        }
}

I have in logcat lines:

D/MY_LOG_1﹕ TEXT_WHICH_WAS_HERE_BEFORE
D/MY_LOG_2﹕ CustomText

So, my code do changes in specific fields, but why I see on my screen still the same text over and over? (in this example is: "TEXT_WHICH_WAS_HERE_BEFORE")

I tried to add

adapter.notifyDataSetChanged();

But it doesn't make any changes...

If someone could help me I'll be very gratefull ;)

Have a nice day!

mariusz-
  • 209
  • 1
  • 10
  • maybe this SO question about [Strikethrough an ArrayList item when long pressed](http://stackoverflow.com/questions/32585249) can help you, my answer there explains how to use a custom adapter and override the "getView()" method. – Bö macht Blau Dec 16 '15 at 15:37
  • I'm not sure, but I can't see the difference here. I think in my solution I get proper view by calling .getAdapter().getView(position,null,null). It's proper because value in TextView are good. But there is a problem somewhere after I guess... – mariusz- Dec 16 '15 at 21:04
  • 1
    You get the proper view, but you should keep in mind that the ListView row is just like a mask your data wear when appearing on screen. You are painting the mask, which will be recycled for performance reasons. So you get the same text over and over. In short: the STRIKE_THRU_TEXT_FLAG has to be set (and "unset") in the getView() method, depending on the underlying data. In the OnClickListener, you should **only** change the underlying data, then call "notifyDatasetChanged()". This will result in "getView()" being called. – Bö macht Blau Dec 17 '15 at 10:51

0 Answers0