mardi 5 mai 2015

Android SQLite Cursor always points to same row

I am looking to build an application wherein the cursor should point to a row containing a particular value of "search".

My table has the following columns : id,title,author,vote,search.

I am using the following code for this :

public ArrayList<String> getTitleDetails(Context c, String search) throws JSONException {
    DatabaseWrapper databaseWrapper = new DatabaseWrapper(c);
    myDataBase = databaseWrapper.getWritableDatabase();
    ArrayList<String> items = new ArrayList<String>();
    Cursor cur = myDataBase.rawQuery("SELECT * FROM question WHERE search" + "= '" + search + "'", null);
    //cur.moveToFirst();
    for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
        if (cur.getString(5).equalsIgnoreCase(search)) {
            JSONObject json = new JSONObject(cur.getString(2));
            JSONArray jarr = json.optJSONArray("uniqueTitles");
            for (int i = 0; i < jarr.length(); i++) {
                items.add(jarr.getString(i));
            }
        }
    }

    return items;
}

and similar other methods for authors,ids etc. The last column i.e. search gets values from a search view.

While this works fine, I noticed that irrespective of the search query, it is always the first row which is returned and the details of those are returned. How do I fix this ?

Thanks !

Aucun commentaire:

Enregistrer un commentaire