Android sqlite query return cursor with 0 results

I am calling AsyncTask passing in a method to be executed to make sure the method returns a name string NAME

from a specific identifier. when calling a database query / raw query on doInBackground, it returns a cursor with null results even when trying with the query SELECT * FROM TABLE

when TABLE has 8 records. What is the problem? Is the request out of sync? if so how should I code it to prevent such problems?

EDIT

...
final TextView tv = (TextView)findViewById(R.id.textView);

dao.retrieveMovieByIdAsync(1, new AsyncTaskCaller() {
    @Override
    public void execute(Movie result) {
        tv.setText(result.get_Name());
    }});
...

private class AsyncRetieveQuery extends AsyncTask<Method, Void, Movie>
{
    private Object args;
    private AsyncTaskCaller signal;

    public AsyncRetieveQuery(Object args, AsyncTaskCaller signal) {
        this.args = args;
        this.signal = signal;
    }

    @Override
    protected Movie doInBackground(Method... params) {
        params[0].setAccessible(true);
        try {
            return (Movie) params[0].invoke(DBUtils.this, new Object[] {args});
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(Movie result) {
        signal.execute(result);
        super.onPostExecute(result);
    }
}
...
public void retrieveMovieByIdAsync(int id, AsyncTaskCaller resultMethod)
{
    try {
        new AsyncRetieveQuery(id,resultMethod).execute(DBUtils.class.getDeclaredMethod("retrieveMovieById", int.class));
    } catch (NoSuchMethodException e) {}
}
...
private Movie retrieveMovieById(int id)
{
    Cursor cursor = database.rawQuery("SELECT * FROM " + SQLiteHelper.TABLE_MOVIES_NAME + " WHERE _Id = ?", new String[] {String.valueOf(id)});

      

The debugger always shows it with zero results

+3


source to share


1 answer


the actual problem is that I had to call cursor.moveToFirst()

even when I only needed to get the line count from the response.
I think that when Google said that the cursor gets the last result after calling the query, it probably means the null string that you are checking with the method isAfterLast

when you iterate over the lines.



0


source







All Articles