-4
    if(cursor.getCount() !=0)
    {

        if(cursor.moveToFirst())
        {
            do
            {
                pObj.gritSize=cursor.getString(cursor
                        .getColumnIndex("grit"));
                pObj.test = cursor.getString(cursor
                        .getColumnIndex("sieve"));
                pObj.number = cursor.getString(cursor
                        .getColumnIndex("number"));
                pObj.micro = cursor.getString(cursor
                        .getColumnIndex("micro"));
                pObj.ret = cursor.getString(cursor
                        .getColumnIndex("retention"));
                pList.add(pObj);

            }while(cursor.moveToNext());
        }
    }
    return pList;

I have the above code that fetches data from table. I am getting correct values in each iteration in pObj. But when i add pList.add(pObj); each time, it is overwriting the previous entry and adding the value. Hence at the end of the iteration i am getting last row as each item in the arraylist. Please help.

4

2 回答 2

7

为每次迭代创建一个新实例:

do {
     pObj = new YourObjectType();
     ....
} while (...);
于 2013-08-29T18:10:15.497 回答
3

您一遍又一遍地使用相同的对象。每次迭代时尝试创建新对象。

do循环顶部初始化它。例如;

pObj = new YourClass();

于 2013-08-29T18:12:25.990 回答