问题
I am a beginner in Android, I have a ListView which loads data from SQLite database with this code on onCreate:
Cursor cursor = dbAdapter.getAllTasks();
String[] fromFields = new String[] {dbAdapter.dbHelper.KEY_TASK};
int[] toView = new int[] {R.id.task};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.task_items, cursor, fromFields, toView, 0);
ListView myList = (ListView) findViewById(R.id.taskList);
myList.setAdapter(myCursorAdapter);
I have db fields task (text), done (boolean) and date (text).
I can toggle strike through in the TextView on item click of ListView using this code and I can change the db field done value here:
ListView myList = (ListView) findViewById(R.id.taskList);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView v = (TextView) view.findViewById(R.id.task);
if ((v.getPaintFlags() & Paint.STRIKE_THRU_TEXT_FLAG) > 0){
v.setPaintFlags( v.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
} else {
v.setPaintFlags(v.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
// calling update method to change done value in db
}
});
Now how can I strike through all items which is marked as done (done = 1) when loading ListView.
回答1:
Since you are using SimpleCursorAdapter, attach a ViewBinder to it that applies your strikethrough. Your setViewValue() would examine your Cursor to see if it is done or not, then would call setPaintFlags() accordingly on the TextView (downcast from the View that is passed in). In addition, you would either need to set the text on the TextView yourself, or specifically return false to indicate that default binding should be applied.
来源:https://stackoverflow.com/questions/27722794/strike-through-textview-item-in-listview-by-checking-database-status