Мой проект используется для сохранения товаров в ShoppingCart в базе данных с dbSqlite
. Но сейчас я должен сохранить информацию о продукте с сервера. Согласно веб-сервису, один тип переменной - List Array
. Это было String
раньше. И один из них - double
, также String
. Я изменил real
в dbSqlite
, но что мне делать для Array
? Как снова сохранить его на dbSqlite?
ошибка: не найден подходящий метод для метода put (String, List) ContentValues.put (String, String) не применим
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;
}
}