2

Why moveToNext() should be given after getString()? If it wasn't, app crashes!

for(int i=0;i<c.getCount();i++)
{
    result+= c.getString(c.getColumnIndex("productname"));                 
    c.moveToNext();
}
Pang
  • 9,564
  • 146
  • 81
  • 122

1 Answers1

0
if (c.moveToFirst()){
   while(!c.isAfterLast()){
      String productNames[i] = c.getString(c.getColumnIndex("productname"));
//Maybe a string array is better than one long string?
      i++
      c.moveToNext();
   }
}
c.close();

How to retrieve data from cursor class

See Some Noob Student 's answer about syntax. You're not using the 'i' index in your code above to specify what row you are reading from - only the number of times you concatenate the product names string. It reads the same row n count times!

use of cursor in android

This question has a good answer explaining what a cursor is. I think of it as a temporary table (Spreadsheet).

movetoNext() moves the 'cursor' (think highlighted spreadsheet rectangle) to the next row of "productname" column (the next product's name).

It could be crashing for a lot of reasons, maybe productname is not a column in cursor 'c'. Here is a question about debugging that may be useful How to debug android project? The old step through then examine the last exception/error method should do the trick.

Community
  • 1
  • 1
MrPickles7
  • 654
  • 2
  • 10
  • 21