Cursor issue in SimpleCursorAdapter - android

Followed this tutorial, faced cursor problem in SimpleCursorAdapter. In tutorial example working as intended. In my code it is showing an error "The constructor SimpleCursorAdapter is undefined". No idea what was undefined. Code look like this:
Cursor cursor = null;
if (inputText == null || inputText.length() == 0) {
cursor = myDataBase.query(true, GL_TABLE, new String[] { GL_ID,
GL_FK, GL_LANG, GL_VALUE}, GL_FK
+ " like '%" + inputText + "%'", null, null, null, null,
null);
} else {
cursor = myDataBase.query(true, GL_TABLE, new String[] { GL_ID,
GL_FK, GL_LANG, GL_VALUE}, GL_VALUE
+ " like '%" + inputText + "%'", null, null, null, null,
null);
}
if (cursor != null) {
cursor.moveToFirst();
}
String[] columns = new String[] { GL_FK, GL_LANG, GL_VALUE};
int[] to = new int[] { R.id.tvWord, R.id.tvMeaning, R.id.tvKanji};
dataAdapter = new SimpleCursorAdapter(this, R.layout.listword,
cursor, columns, to, 0);

Did you import the class ?
import android.widget.SimpleCursorAdapter;
If you did, then be sure to call the constructor with the right kind of parameters. "this" should stand for an activity or a context. if you are in a Runnable or in a clicklistener you will have to call the super MyActivty.this

Related

IllegalArgumentException in filtering ListView

I am using this code to filter my listview:
Cursor fetchCountriesByName(String inputText) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor;
if (inputText == null || inputText.length () == 0) {
String[] columns = new String[] {DBHelper.ID,DBHelper.FNAME,DBHelper.LNAME,DBHelper.ADDRESS };
cursor = db.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null);
}
else {
cursor = db.query(true, TABLE_NAME, new String[] {DBHelper.ID,
DBHelper.FNAME,DBHelper.LNAME},DBHelper.ADDRESS+ " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
I got an error
java.lang.IllegalArgumentException: column 'patient_address' does not exist
But the column names are same. There is a column called patient_address
What's the reason of this error? Please guide.
Try this
cursor = db.query(true, TABLE_NAME, new String[] {DBHelper.ID,
DBHelper.FNAME,DBHelper.LNAME},DBHelper.ADDRESS+ " like '%?%'", new String[]{inputText },
null, null, null, null);
the parameter is error.

filter a listview populated from SQLite

i added a filter to my listview ( populated from sqlite ). i am using this method to fetch the records from sqlite
public Cursor fetchServicesByName(String inputText, String c) throws SQLException {
db = this.getReadableDatabase();
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = db.query(TABLE_SERVICE, new String[] {KEY_ID,KEY_NAME,
KEY_ADRESSE, KEY_EMAIL, KEY_NUMTEL, KEY_WEBSITE,KEY_IMAGE}, KEY_TYPE + "=?",
new String[] { c },null, null, null, null);
}
else {
mCursor = db.query(true, TABLE_SERVICE, new String[] {KEY_ID,KEY_NAME,
KEY_ADRESSE, KEY_EMAIL, KEY_NUMTEL, KEY_WEBSITE,KEY_IMAGE},
KEY_NAME + " like '%" + inputText + "%'" , null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
this is working, but what i want to do is to filter by inputText and KEY_TYPE
i used this (instead of the last db.query ) but it didn't return anything
mCursor = db.query(TABLE_SERVICE, new String[] {KEY_ID,KEY_NAME,
KEY_ADRESSE, KEY_EMAIL, KEY_NUMTEL, KEY_WEBSITE,KEY_IMAGE},
KEY_NAME + " like ? and" + KEY_TYPE + " like ? " , new String[] { inputText, c},
null, null, null, null);
You need to check if your inputtext and keytype are null!
Aris your routine working when you fill out your inputtext and c vallue?

where "not null" clause in sqlite android

I want to retrieve notes from database where notes are (not null), I wrote the following code:
public Cursor getmynotesss(int id){
Cursor mCursor = database.query(true, NOTES_TABLE, new String[] {
NOTE_TEXT, PAGE_NO,BOOK_ID},BOOK_ID + "=? AND "+NOTE_TEXT+" IS NOT NULL OR "+NOTE_TEXT+" != ''" ,
new String[] { String.valueOf(id) }, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
but it still gives me the null notes.
what is the wrong in my code?
It looks like the OR condition should be AND :
"=? AND "+NOTE_TEXT+" IS NOT NULL AND "+NOTE_TEXT+" != ''"
i have solve my problem like this
SELECT pk_video_master_id AS _id,
video_title,
video_added_date,
video_thumbnail FROM tbl_video_master
WHERE NOT video_detail_last_view ='' // this is return not null values and work for me
ORDER BY video_detail_last_view ASC";
so you can try like this
Cursor mCursor = database.query(true, NOTES_TABLE,new String[] {NOTE_TEXT,AGE_NO,BOOK_ID},BOOK_ID + "=? AND NOT "+NOTE_TEXT+" = ''" ,new String[] {String.valueOf(id) }, null, null, null, null);
try
Cursor mCursor = database.query(true, NOTES_TABLE, new String[] {
NOTE_TEXT, PAGE_NO,BOOK_ID}, NOTE_TEXT+" IS NOT NULL AND "+BOOK_ID + "=?" ,
new String[] { String.valueOf(id) }, null, null, null, null);
and not equals in SQLITE is "<>" not "!="
try replacing
IS NOT NULL
with
<>``

SQL cursor error

I have been using the notepad SQLhelper (notesDBadapter) as a model, some of it works, some doesn't. I can get a cursor for 'fetchallrecords() but it crashes if I try a call passing an argument and using the 'WHERE'. The argument is passed but the cursor fails. My code in activity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listselectedfile);
//Button ID clicked in previous activity
Bundle bundle = getIntent().getExtras();
int BtnId = bundle.getInt("ButtonID");
Toast.makeText(this, "ButtonID selected in Main:= " + BtnId, Toast.LENGTH_LONG) .show();
mDbHelper = new SectionsDbAdapter(this);
mDbHelper.open();
fillData();
}
private void fillData() {
// Get all of the notes from the database and create the item list
//Cursor c = mDbHelper.fetchAllRecords(); <=== works fine
Cursor c = mDbHelper.fetchRecordsbySource("UK"); <=== fails in DBhelper
startManagingCursor(c);
String[] from = new String[] { SectionsDbAdapter.KEY_DESC };
//String[] from = new String[] { SectionsDbAdapter.KEY_SOURCE }; <=== can fetch this column from table
int[] to = new int[] { R.id.tv_full_width }; //the R.id.xxx= view in the .xml file
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter records =
new SimpleCursorAdapter(this, R.layout.section_row_full_width, c, from, to); //the .xml file containing the R.id.xxxx
setListAdapter(records);
}
In the DBhelper;
This call works and returns the full table;
public Cursor fetchAllRecords() {
return mDb.query(DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_DESC, KEY_DEPTH, KEY_TWEB,
KEY_BF1, KEY_TF1, KEY_BF2, KEY_TF2,
KEY_IMAJOR, KEY_IMINOR,
KEY_PLMAJOR, KEY_PLMINOR,
KEY_JTORSION, KEY_AREA,
KEY_WARPING, KEY_CYCHANNEL,
KEY_SHAPE, KEY_SOURCE,
KEY_UNITS},null, null, null, null, null);
}
This call fails on the cursor (the argument is passed successfully);
public Cursor fetchRecordsbySource(String source) throws SQLException {
Log.v("In fetchRecordsbySource", "source = " +source);
Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_DESC}, KEY_SOURCE + " = " + source, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
There is nothing obvious to me in the Eclipse debug, but, as a newby, I probably have not got the necessary perspective.
Can anyone spot the error?
In the code you have, if source is a non-numeric value, it needs to surrounded by single quotes, like this:
KEY_ROWID, KEY_DESC}, KEY_SOURCE + " = '" + source + "'", null, null, null, null, null)
But you will be better off if you pass your filter arguments as ?; this avoids (among other things) SQL injection attacks
KEY_ROWID, KEY_DESC}, KEY_SOURCE + " = ?", new String[] { source }, null, null, null, null)

How to implement where clause on Android Query method?

I'm trying to query a word, for this I'm using the db.query method. But I want to use a WHERE clause. I've done like this on my code, but I guess there is something wrong there with my WHERE clause.
public String select(String wrd) {
String list = new String();
Cursor cursor = this.db.query(TABLE_NAME, new String[] {"word"},
"word like " + wrd + "", null, null, null, "id desc");
if (cursor.moveToNext()) {
do {
list = cursor.getString(0);
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return list;
}
I'm calling this method in other class, parsing a String argument.
Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null, null);
String body;
if(cursor.moveToFirst()){
body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
if(body == ""){
Toast.makeText(getBaseContext(), "There is no words to save!", Toast.LENGTH_LONG).show();
}
else{
String words = this.dh.select("Testando");
StringTokenizer st = new StringTokenizer(body);
while(st.hasMoreTokens()){
if(words == st.nextToken())
Toast.makeText(getBaseContext(), "found! "+words+"", Toast.LENGTH_LONG).show();
this.dh.insert(st.nextToken());
Toast.makeText(getBaseContext(), "The set of words has been updated!", Toast.LENGTH_LONG).show();
}
StringBuilder sb = new StringBuilder();
sb.append("Set of words:\n\n");
sb.append(words + " ");
txtView.setText(sb.toString());
}
}
I believe the problem has to do with the double-quotes in the below code:
Cursor cursor = this.db.query(TABLE_NAME, new String[] {"word"},
"word like " + wrd + "", null, null, null, "id desc");
Try this
Cursor cursor = this.db.query(TABLE_NAME, new String[] {"word"},
"word like \"" + wrd + "\"", null, null, null, "id desc");
Or even better
Cursor cursor = this.db.query(TABLE_NAME, new String[] {"word"},
"word like ?",new String[] {wrd} , null, null, "id desc");
Edit:
Use single quote instead of double-quote in your WHERE clause. See here
Cursor cursor = this.db.query(TABLE_NAME, new String[] {"word"},
"word like \'" + wrd + "\'", null, null, null, "id desc");

Categories

Resources