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!