androidsqlite

Multiple columns in an array list


I am trying to fetch all the columns from my SQLite database to an ArrayList. My table name is Products. I have created an ArrayList adapter name Product. I am fetching my tables value in DBHELPER class. But when I am using ArrayList add to add all the columns value in my arrayadapter I get the following error:

Cannot resolve method add(java.lang.string, java.lang.string)

DBHELPER.JAVA

public ArrayList<Product> getProductdetails()
{
    ArrayList<Product> array_list = new ArrayList<Product>();
    SQLiteDatabase db = this.getReadableDatabase();
    db.beginTransaction();
    try {
        String selectQuery = "SELECT  * FROM " + TABLE_PRODUCTS;
        Cursor cursor = db.rawQuery(selectQuery, null);


        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {

                String product_name = cursor.getString(cursor.getColumnIndex("PRODUCT_NAME"));
                String Quantity = cursor.getString(cursor.getColumnIndex("Quantity"));
                String PROD_VAT = cursor.getString(cursor.getColumnIndex("PROD_VAT"));

                array_list.add(product_name,Quantity,PROD_VAT,PROD_VAT);
               // array_list.addAll(Arrays.asList(product_name,Quantity,PROD_VAT,PROD_VAT));
            }
        }

    }catch (Exception e){
        e.printStackTrace();
    }
    finally {
        db.endTransaction();
        db.close();
    }
    return array_list;
}

PRODUCT.JAVA

 public class Product {
//private variables

String _PRODUCT_CODE;
String _PRODUCT_NAME;
String _PRODUCT_RATE;
String _PRODUCT_VAT;
// Empty constructor
public Product(){

}
// constructor
public Product(String PRODUCT_CODE, String PRODUCT_NAME, String PRODUCT_RATE,String PRODUCT_VAT){
    this._PRODUCT_CODE = PRODUCT_CODE;
    this._PRODUCT_NAME = PRODUCT_NAME;
    this._PRODUCT_RATE = PRODUCT_RATE;
    this._PRODUCT_VAT = PRODUCT_VAT;
}


public String getproductcode(){ return this._PRODUCT_CODE;}

public String getproductname(){
    return this._PRODUCT_NAME;
}
public String getproductrate(){return this._PRODUCT_RATE;}
public String getproductvat(){return this._PRODUCT_VAT;}
}

Solution

  • You are adding data to ArrayList in wrong way. Your ArrayList is type of object (eg. your Product class), So you need to pass instance of model class.

    array_list.add(new Product(code,name,rate,vat));