1

Here is my short code i am inserting 10 rows into table but after complete loop why i have same visualisation like "0 Net 0" ten time ? //Table Structure

 String creater = "CREATE TABLE IF NOT EXISTS est (ID INTEGER PRIMARY KEY AUTOINCREMENT,codigo VARCHAR(10),"
    + "produto VARCHAR(1000), "
    + "qtd VARCHAR(10) ) ";
    sql.execSQL(creater);

//Loop of Insertion

   for(int i=0;i<10;i++)
    {
    ContentValues values=new ContentValues(); 
    values.put("codigo", String.valueOf(i));
    values.put("produto", "Net");
    values.put("qtd", String.valueOf(i));
    sql.insert("est", null, values);
    }

//ArrayList

public  ArrayList<String>  estoque(SQLiteDatabase sql)
     {
    ArrayList<String> lst = new ArrayList<String>();
    try{
    String cmd ="select * from est";
    Cursor c = sql.rawQuery(cmd,null);
    c.moveToFirst();
    int i=0;
    while(c.getCount() > i )
    {
    lst.add(c.getString(1)+"|"+c.getString(2)+"|"+c.getString(3));
    i++;
    }
    }catch(Exception ex){Log.e("ERROR", ex.getStackTrace().toString());}
    return lst;
    };

//ListView

 ListView  listview =(ListView)findViewById(R.id.listview);
ArrayList<String> estoque = new db().estoque(sql);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, estoque);
adapter1.setDropDownViewResource(android.R.layout.simple_list_item_1);
listview.setAdapter(adapter1);

//visualisation "0 Net 0" 10 time same. is there somthing wrong please help?

5
  • Where do you visualize the data? Commented Sep 24, 2013 at 13:37
  • String cmd ="select * from est"; Cursor c = sql.rawQuery(cmd,null); c.moveToFirst(); int i=0; while(c.getCount() > i ) { lst.add(c.getString(1)+"|"+c.getString(2)+"|"+c.getString(3)); i++; } // i am getting all records like this code Commented Sep 24, 2013 at 13:38
  • Please edit your question and add that information. It's hard to read a lot of code in a comment. Commented Sep 24, 2013 at 13:40
  • check now where i have problem? Commented Sep 24, 2013 at 13:45
  • Read my answer below. Commented Sep 24, 2013 at 13:47

1 Answer 1

1

In your while loop where you read from the database (either before or after you increase the i variable), you should call c.moveToNext();. Otherwise you are reading from the same row over and over again.

The documentation states:

public abstract boolean moveToNext ()
Move the cursor to the next row.
This method will return false if the cursor is already past the last entry in the result set.

Returns
    whether the move succeeded. 
Sign up to request clarification or add additional context in comments.

Comments

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.