gpt4 book ai didi

java - 如何将数组值放入 db Sqlite 上的数据库?

转载 作者:行者123 更新时间:2023-12-01 23:50:56 25 4
gpt4 key购买 nike

我的项目用于使用dbSqlite将产品信息保存到数据库中的shoppingCart中。但是,现在它应该从服务器保存产品信息。根据 Web 服务,一种变量类型是List Array。之前是String。其中之一是double,它也是String。我在 dbSqlite 中更改了 real,但我应该对 Array 做什么?如何再次将其保存到dbSqlite上?

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

购物车.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;
}
}

最佳答案

您可以像这样读取数组:

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);
}

关于java - 如何将数组值放入 db Sqlite 上的数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58220897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com