按ID检索记录无效?

时间:2016-02-10 12:22:28

标签: android sqlite

我试图根据用户ID显示记录但是它的最后更新记录只能显示基于id的整个记录​​我怎样才能实现这一点让我发布我的代码:

以下是我根据id:

检索记录的代码
  public List<Model_Task_List> getTaskListById(String id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Model_Task_List model_task_list=new Model_Task_List();
        String selectQuery =  " SELECT  " +
                Model_Task_List.KEY_id + "," +
                Model_Task_List.KEY_username + "," +
                Model_Task_List.KEY_subject + "," +
                Model_Task_List.KEY_task + "," +
                Model_Task_List.KEY_taskid  +"," +
                Model_Task_List.KEY_owner+
                " FROM " + Model_Task_List.KEY_table + " WHERE "   +  Model_Task_List.KEY_owner + "= " + id  + "";
        List<Model_Task_List>listobj=new ArrayList<Model_Task_List>();
        Model_Task_List modelobj=new Model_Task_List();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
           do {
               modelobj.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Model_Task_List.KEY_id))));
               modelobj.setUserName(cursor.getString(cursor.getColumnIndex(Model_Task_List.KEY_username)));
               modelobj.setSubject(cursor.getString(cursor.getColumnIndex(Model_Task_List.KEY_subject)));
               modelobj.setTaskStatus(cursor.getString(cursor.getColumnIndex(Model_Task_List.KEY_task)));
               modelobj.setTaskID(cursor.getShort(cursor.getColumnIndex(Model_Task_List.KEY_taskid)));
               modelobj.setUserid(cursor.getString(cursor.getColumnIndex(Model_Task_List.KEY_owner)));
               listobj.add(modelobj);
            }while (cursor.moveToNext());
        }

        return listobj;

    }

以下是我设置适配器的代码:

model_task_lists=database_sf_app.getTaskListById(user_id)
taskadapter=new Task_List_Adapter(model_task_lists,getActivity()); recyclerView.setAdapter(taskadapter);

如何以正确的方式对此问题进行排序

1 个答案:

答案 0 :(得分:2)

问题只有你的模型类,你只创建一个模型类对象一次。 如果检索全部数据库记录,则每次获取记录时都会创建一个新对象。

    if (cursor.moveToFirst()) {
       do {
modelobj=new Model_Task_List();            modelobj.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Model_Task_List.KEY_id))));
           modelobj.setUserName(cursor.getString(cursor.getColumnIndex(Model_Task_List.KEY_username)));
           modelobj.setSubject(cursor.getString(cursor.getColumnIndex(Model_Task_List.KEY_subject)));
           modelobj.setTaskStatus(cursor.getString(cursor.getColumnIndex(Model_Task_List.KEY_task)));
           modelobj.setTaskID(cursor.getShort(cursor.getColumnIndex(Model_Task_List.KEY_taskid)));
           modelobj.setUserid(cursor.getString(cursor.getColumnIndex(Model_Task_List.KEY_owner)));
           listobj.add(modelobj);
        }while (cursor.moveToNext());
    }