Search code examples
javaandroidandroid-sqlite

How to put Array value to Database on dbSqlite?


My project used to save products information into shoppingCart on a database with dbSqlite. But, for now it should save information of product from server. According to web service, one variable type is List Array. It was String before. And one of them is double, it was String also. I changed real in dbSqlite but what I should do for Array? How can save it on dbSqlite again?

error: no suitable method found for put(String,List) method ContentValues.put(String,String) is not applicable

Cart.java

public class Cart {
    private List<String> Image;
    private String Title;
    private double Cost;
    private String Market;

    public List<String> getImage() {
        return Image;
    }

    public void setImage(List<String> image) {
        Image = image;
    }

    public double getCost() {
        return Cost;
    }

    public void setCost(double cost) {
        Cost = cost;
    }

    private String TotalCost;
    private String Description;

    public boolean boolExpand = false;

    boolean isExpanded;


    public boolean isExpanded() {
        return this.isExpanded;
    }

    public void setExpanded(boolean expanded) {
        this.isExpanded = expanded;
    }




    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }



    public String getMarket() {
        return Market;
    }

    public void setMarket(String market) {
        Market = market;
    }

    public String getTotalCost() {
        return TotalCost;
    }

    public void setTotalCost(String totalCost) {
        TotalCost = totalCost;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }
}

ProductDetailAdapter.java



public class ProductDetailAdapter extends RecyclerView.Adapter<ProductDetailAdapter.ViewHolder> {
 btnProductDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
                // isLogined bilgisi shared preferences'tan alınıyor. eğer true ise username ve password bilgileri alınıyor.
                // değil ise empty olarak giriliyor.
                isLogined = sharedPreferences.getBoolean("isLogined", false);
                if (isLogined) {
                    saveCartAmount();
                    db = new DatabaseHelper(mContext);
                    List<String> img;
                    double cost;
                    String title;
                    String total;
                    String market;
                    String description;
                    img = productPageList.get(position).getProductImages();
                    cost = productPageList.get(position).getProductPrices().get(position).getShopProductPrice();
                    title = productPageList.get(position).getProductName();
                    market = productPageList.get(position).getProductPrices().get(position).getShopName();
                    total = "";
                    description = productPageList.get(position).getProductDescription();
                    // Log.e("amountdb","dasdsa");
                    //  amount = amountdb++;

                    db.AddToCart(img, title, cost, market, total, description);

                    cartListener.onProductSelect(productPageList.get(position));

                    Toast.makeText(mContext, "Ürün sepetinize eklendi.", Toast.LENGTH_SHORT).show();

                }
                else{
                    Toast.makeText(mContext, "Sepete ürün eklemek için üye girişi yapmanız gerekmektedir.", Toast.LENGTH_SHORT).show();
                }
            }
        });
   }

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper(Context context) {
        super(context, "Login.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
       db.execSQL("Create table cart(Image text, Title text, Cost real, Market text, TotalCost text, Description text)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

      db.execSQL("drop table if exists cart");
    }

    public void deleteCart(){
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete("cart", null,null);
    }
    public void AddToCart(List<String> Image, String Title, Double Cost, String Market, String TotalCost, String Descritpion){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues data = new ContentValues();

        data.put("Image",Image );
        data.put("Title",Title);
        data.put("Cost",Cost);
        data.put("Market",Market);
        data.put("TotalCost", TotalCost);
        data.put("Description", Descritpion);

        db.insert("cart",null,data);

    }

    public List<Cart> getdata(){
        // DataModel dataModel = new DataModel();
        List<Cart> data=new ArrayList<>();
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor rs =  db.rawQuery( "select * from cart",null);
        StringBuffer stringBuffer = new StringBuffer();
        Cart dataModel = null;
        while (rs.moveToNext()) {
            dataModel= new Cart();
            List<String> image = Collections.singletonList(rs.getString(rs.getColumnIndexOrThrow("Image")));
            String title = rs.getString(rs.getColumnIndexOrThrow("Title"));
            Double cost = (rs.getDouble(rs.getColumnIndexOrThrow("Cost")));
            String market = rs.getString(rs.getColumnIndexOrThrow("Market"));
            String totalCost = rs.getString(rs.getColumnIndexOrThrow("TotalCost"));
            String description = rs.getString(rs.getColumnIndexOrThrow("Description"));

            dataModel.setImage(image);
            dataModel.setTitle(title);
            dataModel.setCost(cost);
            dataModel.setMarket(market);
            dataModel.setTotalCost(totalCost);
            dataModel.setDescription(description);
            stringBuffer.append(dataModel);
            data.add(dataModel);
        }

        return data;
    }
}

Solution

  • You can read the array like this :

    String[] myArray = `put your array here`
    for (int i = 0; i < myArray.size(); i++)
    {
     //Here u insert the data by getting what u need from the array
     data.put("Cost",myArray.get(i).Cost);
    }