Search code examples
androiddatabasesqlitecursor

Unable to fetch the first entry in SQLlite Database using cursors in android


I am trying to fetch all the entries in a database table using SQLite. I ran a query, stored the values in a cursor and then via a loop I fetched all the values. However I can access all the entries except for the first one. Here is my code :

mydb1=new Database_CustomTransaction(getApplicationContext());
        Cursor c12 = mydb1.executeQuery("Select * from table1");

        System.out.println(c12);
        if(c12 == null)
        {
             TextView nodataView = new TextView(this);
             nodataView.setId(20);
             nodataView.setText("No Data here !");
             nodataView.setTextSize(20);
        }
        else
        {
            if(flagValue == false)
            {
                c12.moveToFirst();
                flagValue = true;
            }

            while(c12.moveToNext())
            {
                type=c12.getString(0);
                amount = c12.getInt(1);
                spentOn = c12.getString(2);
                date = c12.getString(3);

                listType.add(i,type);
                listSpentOn.add(i,spentOn);
                listAmount.add(i,amount);
                listDate.add(i,date);
                i++;
            }

        }
        latesttrans2.setAdapter(new TestAdapter2(this, listType, listSpentOn,listAmount,listDate)); 

Any ideas what I am doing wrong ?


Solution

  • c12.moveToFirst();
    

    This moves to the first row.

    while(c12.moveToNext())
    

    This moves to the next row after the first row.

    I would guess that the first call should be just dropped, but only you know what you intended with flagValue.