Jump to content

Android/SQLite get all items from one column

Flanelman

Hey Guys, so I want to get every category that is saved in my Database and save it into an ArrayList<String> in my main activity but I'm unsure of how to do so. Here's what I've tried:

 

//in the database manager class

 public ArrayList<String> get_all_Category()
    {
        SQLiteDatabase db = getWritableDatabase();
        
        ArrayList<String> all_categories = new ArrayList<String>();
        String dbString = "";
    
        String get_all_cat_query = " SELECT " + COLUMN_CATEGORY + " FROM " + TABLE_ITEMS + " WHERE 1 "; 

        Cursor cursor = db.rawQuery(get_all_cat_query, null);
        cursor.moveToFirst(); 
        while(!cursor.isAfterLast()) //while there is still data to check
        {
            if(cursor.getString(cursor.getColumnIndex("item_index"))!=null); 
            {
                dbString = cursor.getString(cursor.getColumnIndex("item_category"));
                all_categories.add(dbString);

            }
        }
        return all_categories;
    }
//in my main class

        db_manager = new databaseManager(this, null, null, 1); //create the database
        long index_value = db_manager.Get_Length_of_DB();
        int index = (int) index_value;
        if(index != 0) 
        {
            category_List.add(db_manager.get_all_Category().toString());
        }

 

I'm getting this error on this code which I don't really understand: 

 

Failed to read row 0, column -1 from a CursorWindow which has 2 rows, 1 columns.
java.lang.RuntimeException: Unable to start activity ComponentInfo{bit.powlz1.zak_mobileassignment/bit.powlz1.zak_mobileassignment.NewItemScreen}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.


Any help would be greatly appreciated! :)

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Flanelman said:

Hey Guys, so I want to get every category that is saved in my Database and save it into an ArrayList<String> in my main activity but I'm unsure of how to do so. Here's what I've tried:
 

I'm getting this error on this code which I don't really understand: 

 


Failed to read row 0, column -1 from a CursorWindow which has 2 rows, 1 columns.
java.lang.RuntimeException: Unable to start activity ComponentInfo{bit.powlz1.zak_mobileassignment/bit.powlz1.zak_mobileassignment.NewItemScreen}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.


Any help would be greatly appreciated! :)

From the error I can imagine that cursor is trying to access a column with a negative index meaning it is not found. In lines:

 

if(cursor.getString(cursor.getColumnIndex("item_index"))!=null); 
{
  dbString = cursor.getString(cursor.getColumnIndex("item_category"));
  all_categories.add(dbString);
}

you're accessing two column's indexes by their names. One of the two "cursor.getColumnIndex" probably returns -1 (it did not find the column) and that's where you're getting the error. Your error also says that your cursor has 2 rows and 1 column, but you're trying to access columns "item_index" and "item_category" so one of them is surely missing.

Try, fail, learn, repeat...

Link to comment
Share on other sites

Link to post
Share on other sites

21 hours ago, Flanelman said:

Hey Guys, so I want to get every category that is saved in my Database and save it into an ArrayList<String> in my main activity but I'm unsure of how to do so. Here's what I've tried:

 


//in the database manager class

 public ArrayList<String> get_all_Category()
    {
        SQLiteDatabase db = getWritableDatabase();
        
        ArrayList<String> all_categories = new ArrayList<String>();
        String dbString = "";
    
        String get_all_cat_query = " SELECT " + COLUMN_CATEGORY + " FROM " + TABLE_ITEMS + " WHERE 1 "; 

        Cursor cursor = db.rawQuery(get_all_cat_query, null);
        cursor.moveToFirst(); 
        while(!cursor.isAfterLast()) //while there is still data to check
        {
            if(cursor.getString(cursor.getColumnIndex("item_index"))!=null); 
            {
                dbString = cursor.getString(cursor.getColumnIndex("item_category"));
                all_categories.add(dbString);

            }
        }
        return all_categories;
    }

//in my main class

        db_manager = new databaseManager(this, null, null, 1); //create the database
        long index_value = db_manager.Get_Length_of_DB();
        int index = (int) index_value;
        if(index != 0) 
        {
            category_List.add(db_manager.get_all_Category().toString());
        }

 

I'm getting this error on this code which I don't really understand: 

 


Failed to read row 0, column -1 from a CursorWindow which has 2 rows, 1 columns.
java.lang.RuntimeException: Unable to start activity ComponentInfo{bit.powlz1.zak_mobileassignment/bit.powlz1.zak_mobileassignment.NewItemScreen}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.


Any help would be greatly appreciated! :)

Also If I am not mistaken your sql query is limiting what is being return. For example, if i am not mistaken should should "SELECT * FROM" +TABLE_ITEMS, (the where clause is optional I would personally use when I need to filteror check/match conditions if not no point in having it.) This wall all results are returned. It may solve your issue then again I do not want to say that is the issue. Also I would recommend using .movetonext() instead of .isafterlast(). Personally this is how I would write the syntax

if(cursor.moveToFirst()){

do{

 if(cursor.getString(cursor.getColumnIndex("item_index"))!=null); 
            {
                dbString = cursor.getString(cursor.getColumnIndex("item_category"));
                all_categories.add(dbString);

            }

}while(cursor.moveToNext());

}

 

No idea if what i stated helps, but i hope it does. If I can help out anymore feel free to contact me.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×