Title: how to make sqlite database query with some condtions
Tags: android;sqlite;cursor
Question: I have a reminder app where I want to show different schedules of programs. There I have four sections
Overview(details of all the schedules)
Tomorrow
10-days-later
Now I can list 'overview' section with the help of cursoradapter. But I cannot list 'today' and rest of the sections--the screen remains blank. Here is my code of todayActivity
in 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);
in 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;
And this is bindView method in my custom cursor adapter class----
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);
So, my question is how to make the cursor of custom cursoradapter know about the certain conditions from database that I need? This may be asked before, but I cannot find a solution for the problem. Please help me. Thanks in advance!!!


Here is another answer: Try using db.query() which has selection and selectionArgs as parameter for conditional selection.
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
);
in your case selction and selectionArgs will be :
String selection = COLUMN_DAY+ " = ? AND"+COLUMN_MONTH+ " = ?
AND"+COLUMN_YEAR+" = ?";
String[] selectionArgs = { day, month, year };
make sure day, month and year are strings.
Comment for this answer: I guess there wont be any change. check what value comes in your cursor. use `DatabaseUtils.dumpCursorToString(cursor)` it return string value that contains data in your cursor. print that string in log to check whether cursor has all values or filtered values or null.
Comment for this answer: same problem. Can you tell me where the change of new conditioned cursor is reflected in the custom cursor adapter class ?
Comment for this answer: day,month and year initialization as I have done---c = Calendar.getInstance();
year = String.valueOf(c.get(Calendar.YEAR)); month = String.valueOf(c.get(Calendar.MONTH));
day = String.valueOf(c.get(Calendar.DAY_OF_MONTH)); Is it right way?

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:06/05/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><sqlite><cursor>', 'Title': 'how to make sqlite database query with some condtions', 'ViewCount': 54, 'id': 50691688, 'user_AboutMe': '', 'user_AccountId': 9869379.0, 'user_DisplayName': 'sayanmanik_sm', 'user_Reputation': 23.0, 'user_Views': 10.0, 'user_id': 7309622.0}}