so I have this huge problem with applying a strikethrough on a ListView item while populating it from the List of my objects.
private void setListItems() {
DatabaseHandler db = new DatabaseHandler(this);
List<ToDoTask> taskList = db.getAllTasks();
for (ToDoTask tt : taskList) {
this.listAdapter.add(tt.getName());
}
this.listAdapter.notifyDataSetChanged();
}
This part of the code populates my ListView. Now i want to add a strikethrough to item if
tt.getActive() == 0
I don't know how can i get a TextView item from my ListView. I tried in my for loop to:
TextView textView = (TextView) this.listView.getChildAt(this.listView.getCount() -1);
but the app crashes. The only solution that didn't crash my app was:
TextView textView = this.listAdapter.getView(this.listView.getCount() -1, null, null)
but it didn't actually change anything in the ListView. I think it's not referencing the object itself. Here's some code that instantiates my variables:
this.listView = (ListView) findViewById(R.id.listViewItem);
this.listItems = new ArrayList<String>();
this.listAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, this.listItems);
this.listView.setAdapter(this.listAdapter);
The list view is not in charge of instantiating or modifying the underlying state of the views it contains. That's the job of the adapter. When you scroll a ListView, it calls the adapter method
getView()
and the adapter will hand it a row (possibly a recycled view from the rows that scrolled off-screen).Inside the
getView()
method, the adapter should consult the underlying data set and modify the row as needed - that would include crossing out text, or setting any visual properties of the row content.