0

I have a table called food. I am selecting the "category" of the food item and I want to show it in a List View. This is the code I tried.

Cursor c = db.query(DATABASE_TABLE,
                new String[] { "category" }, null, null, null, null, null);
        ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();           
        if(c.moveToFirst())
        {
            do
            {   ArrayList<String> recipe = new ArrayList<String>();
                recipe.add(c.getString(1));
                recipe.add(c.getString(2));
                recipe.add(c.getString(3));
                recipe.add(c.getString(4));
                results.add(recipe);
            }while(c.moveToNext());
            if(c != null && !c.isClosed())
               c.close();
        }
2
  • I cannot add all rows to the ListView, I can only add First value of the table. Commented Jun 7, 2012 at 6:49
  • you have to add 4 value in 4 diff array. use placeholder for create list or if you want to use that in single list then concate all value first and add it to in array. Commented Jun 7, 2012 at 6:52

2 Answers 2

1

Try Now,

Cursor c = db.query(DATABASE_TABLE,
                new String[] { "category" }, null, null, null, null, null);
        ArrayList<String> results = new ArrayList<String>();           
        if(c.moveToFirst())
        {
            do
            {   
             results.add(c.getString(0)); // instead of 0 Index of Category column in your case
            }while(c.moveToNext());
            if(c != null && !c.isClosed())
               c.close();
        }

From your database query You are selecting only category column so you have only one column result in cursor and its start with 0 index. So c.getString(1) to c.getString(4) is meaning less. If your select all data from table then only you get all columns..

Sign up to request clarification or add additional context in comments.

Comments

1

I see your problem, you are only returning one column (category), yet you are trying to access several different ones.

You should be returning at least five (since you are trying to access up to 4 and the cursor columns start at 0).

If you are trying to pull a list of items with a certain category you need to change your query. Something like this :

String query = "SELECT * FROM " + DATABASE_TABLE + " WHERE category = " + category;
return mDb.rawQuery(query, null);

That will select all items that have a category matching whatever is contained in the variable category, and return all the columns in the row.

2 Comments

Ya I got it. It should be corrected as recipe.add(c.getString(3)); But How can I access next row?
I'm not sure I understand... access next row? The query returns a cursor which contains columns and rows. You are navigating the rows with moveToFirst and moveToNext in your loop, and accessing the columns in each row with the c.getString(#) calls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.