简体   繁体   English

通过检查数据库状态来遍历ListView中的TextView项目

[英]Strike through TextView item in ListView by checking database status

I am a beginner in Android, I have a ListView which loads data from SQLite database with this code on onCreate : 我是Android的初学者,我有一个ListView ,它使用onCreate上的这段代码从SQLite数据库加载数据:

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) . 我有数据库字段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的项单击上的TextView中切换删除线,并且可以在此处更改db字段的done值:

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. 现在,当加载ListView时,如何删除所有标记为完成的项目( done = 1 )。

Since you are using SimpleCursorAdapter , attach a ViewBinder to it that applies your strikethrough. 由于您使用SimpleCursorAdapter ,附上一个ViewBinder它,你的应用删除线。 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). 您的setViewValue()将检查您的Cursor以查看是否完成,然后在TextView上相应地调用setPaintFlags() (从传入的View中向下转换)。 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. 此外,您可能需要自行设置TextView上的文本,或者专门返回false来指示应应用默认绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM