如何使用一些条件进行sqlite数据库查询

时间:2018-06-05 03:46:04

标签: android sqlite cursor

我有一个提醒应用,我想要显示不同的节目时间表。我有四个部分

  1. 概述(所有时间表的详细信息)
  2. 今天
  3. 明天
  4. 10天以后

    现在我可以列出'概述'在cursoradapter的帮助下。但我今天不能列出'其余部分 - 屏幕保持空白。这是我的todayActivity

    代码

    在oncreate()---

        DatabaseHelper fdb = new DatabaseHelper(this);
        db = fdb.getReadableDatabase();
        cursor = db.rawQuery("select * from "+TABLE_NAME+" where "+COLUMN_DAY+" = "+day+" and "+
                COLUMN_MONTH+" = "+month+" and "+COLUMN_YEAR+" = "+year+";",null);
    
        adapter = new AppointmentAdapter(this, cursor);
        listView.setAdapter(adapter);
    
  5. 在onresume()----

            DatabaseHelper fdb = new DatabaseHelper(this);
            SQLiteDatabase db = fdb.getReadableDatabase();
            Cursor newCursor = db.rawQuery("select * from "+TABLE_NAME+" where "+COLUMN_DAY+" = "+day+" and "+
                    COLUMN_MONTH+" = "+month+" and "+COLUMN_YEAR+" = "+year+";",null);
    
            //RecyclerView recyclerView=findViewById(R.id.recycler);
            AppointmentAdapter adapter = (AppointmentAdapter)listView.getAdapter();
    
            adapter.changeCursor(newCursor);
            cursor = newCursor;
    

    这是我的自定义游标适配器类----

    中的bindView方法
     public void bindView(View view, Context context, Cursor cursor) {
    
        int drawable = 0;
        ImageView imgView = view.findViewById(R.id.type_image);
        name = cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
        type = cursor.getString(cursor.getColumnIndex(COLUMN_TYPE));
        date = cursor.getString(cursor.getColumnIndex(COLUMN_DAY));
        month = cursor.getString(cursor.getColumnIndex(COLUMN_MONTH));
        year = cursor.getString(cursor.getColumnIndex(COLUMN_YEAR));
        hour = cursor.getString(cursor.getColumnIndex(COLUMN_HOUR));
        minute = cursor.getString(cursor.getColumnIndex(COLUMN_MINUTE));
        phone = cursor.getString(cursor.getColumnIndex(COLUMN_PHONE));
    
        if (type.equals("Health"))
            drawable = R.drawable.icons8_stethoscope_96;
        else if (type.equals("Personal"))
            drawable = R.drawable.icons8_resume_96;
        else if (type.equals("Work"))
            drawable = R.drawable.icons8_business_80;
        else if (type.equals("School"))
            drawable = R.drawable.icons8_school_80;
        else if (type.equals("Other"))
            drawable = R.drawable.icons8_exclamation_mark_48;
    
        imgView.setImageResource(drawable);
    
        TextView textView1 = view.findViewById(R.id.clock_text);
        textView1.setText(hour + ":" + minute);
        TextView textView2 = view.findViewById(R.id.date_text);
        textView2.setText(date + "-" + month + "-" + year);
        TextView textView = view.findViewById(R.id.nameText);
        textView.setText(name);
        TextView textView3 = view.findViewById(R.id.phoneText);
        textView3.setText(phone);
    }
    

    所以,我的问题是如何使自定义cursoradapter的光标知道我需要的数据库中的某些条件?这可能会在之前被问到,但我无法找到问题的解决方案。请帮我。在此先感谢!!!

1 个答案:

答案 0 :(得分:0)

尝试使用db.query(),它具有selection和selectionArgs作为条件选择的参数。

Cursor cursor = db.query(
    TABLE_NAME,   // The table to query
    projection,           // The array of columns to return (pass null to get all)
    selection,            // The columns for the WHERE clause
    selectionArgs,        // The values for the WHERE clause
    null,                 // don't group the rows
    null,                 // don't filter by row groups
    sortOrder             // The sort order
    );

在你的情况下,selction和selectionArgs将是:

 String selection = COLUMN_DAY+ " = ? AND"+COLUMN_MONTH+ " = ? 
 AND"+COLUMN_YEAR+" = ?";

 String[] selectionArgs = { day, month, year };

确保日,月和年是字符串。