Question

I want to pull a row from a data-filled cursor and store it in another object for other use.

my code:

SQLiteDatabase db = dbOpener.getReadableDatabase();
            Cursor dataSet = db.query(WPTemplateDB.PRODUCT_TABLE, 
                                null, //all columns
                                null, //where clause
                                null, //where clause args
                                null, null, null);//groupBy, having, orderBy
while (dataSet.moveToNext()){

                Product product = new Product(dataSet);
                pArray.add(product);
            }

my storing object:

public Product(Cursor cursor){

        productData = cursor;
    }

    public String getData(String column){
        Log.d(column, productData.getColumnIndex(column)+"");
        return productData.getString(productData.getColumnIndex(column));
    }

Now, I am facing an error of "index 10 requested with a size of 10". What can I do to this?

Was it helpful?

Solution 3

Actually, this answer is inspired from @cuasodayleo, I just translate it to be another code.

SQLiteDatabase db = dbOpener.getReadableDatabase();
            Cursor dataSet = db.query(WPTemplateDB.PRODUCT_TABLE, 
                                new String[]{WPTemplateDB.PRODUCT_ID}, 
                                null, //where clause
                                null, //where clause args
                                null, null, null);//groupBy, having, orderBy
            while (dataSet.moveToNext()){

                Product product = new Product(dataSet.getInt(0), db);
                pArray.add(product);
            }
            dataSet.close();

The Product object:

public Product(int pid, SQLiteDatabase db){

        productData = db.query(WPTemplateDB.PRODUCT_TABLE, 
                null, //all columns
                WPTemplateDB.PRODUCT_ID+"=?", //where clause
                new String[]{pid+""}, //where clause args
                null, null, null);//groupBy, having, orderBy    

    }

    public String getData(String column){       
        Log.d("count", productData.getCount()+"");
        if (productData.getCount()>0){
            Log.d(column, productData.getColumnIndex(column)+"");
            productData.moveToFirst();
            return productData.getString(productData.getColumnIndex(column));
        } else {
            return null;
        }       
    }

OTHER TIPS

Don't should using cursor is contructors for Product objects. It will leak memory because cursor must close when not use. You should read data from cursor then send data to contructor Product like:

String productName = cursor.getString(0);// 0 is column name
......
cursor.close();

Product product = new Product(name);

Indexes are zero-based. It means that is size is 10, max index will be 9. From the code you've posted, everything is fine, so the error in some other place of your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top