我的代码未在数据库中写入任何内容

时间:2019-08-08 13:37:15

标签: java sqlite android-studio android-sqlite

我需要为每个项目在database中写入三个值,之后,我需要从SQLite中的数据库中检索数据以在购物车中显示项目,但是并没有。不要在数据库中写入任何内容,而当我尝试检索所有内容时,我得到的是一个空项目,只有没有值的布局

我真的不知道如何使用SQLite,我希望我的代码是正确的

这是onBindViewHolder,我将数据写入database中:

   @Override
        protected void onBindViewHolder(@NonNull FragmentUnuViewHolder fragmentUnuViewHolder, int i, @NonNull final FragmentUnuModel fragmentUnuModel) {

            fragmentUnuViewHolder.denumire.setText(fragmentUnuModel.getDenumire());
            fragmentUnuViewHolder.descriere.setText(fragmentUnuModel.getDescriere());
            fragmentUnuViewHolder.pret.setText(Integer.toString(fragmentUnuModel.getPret())+" RON");

            Picasso.with(getActivity().getBaseContext()).load(fragmentUnuModel.getImagine()).into(fragmentUnuViewHolder.img);
            fragmentItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Database(getActivity().getBaseContext()).addToCart(new OrderModel(

                            fragmentUnuModel.getDenumire(),
                            fragmentUnuModel.getDescriere(),
                            fragmentUnuModel.getPret()

                    ));
                    Toast.makeText(getActivity(),"Added to Cart "+fragmentUnuModel.getDenumire(),Toast.LENGTH_LONG).show();
                }
            });
        }

这是Cart.java

  public class Cart extends AppCompatActivity {

RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;

FirebaseDatabase database;
DatabaseReference request;
TextView txtTotalPrice;

List<OrderModel> cart = new ArrayList<>();

CartAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);

    //Firebase
    database = FirebaseDatabase.getInstance();
    request = database.getReference("Comanda");

    //Init
    recyclerView = (RecyclerView)findViewById(R.id.listCart);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    txtTotalPrice = (TextView)findViewById(R.id.total);

    loadListFood();
}

private void loadListFood() {

    cart = new Database(this).getCarts();
    adapter = new CartAdapter(cart,this);
    recyclerView.setAdapter(adapter);

    //Calc TOTAL PRICE
    int total = 0;
    for(OrderModel order:cart)
        total+=(Integer.parseInt(String.valueOf(order.getPret()))); /* *(Integer.parseInt(order.getQuantity())); */
    Locale locale = new Locale("en","EU");
    NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);

    txtTotalPrice.setText(fmt.format(total));
}

这是CartAdapter.java

 public class CartAdapter extends RecyclerView.Adapter<CartViewHolder> {

private List<OrderModel> listData = new ArrayList<>();
private Context context;

public CartAdapter(List<OrderModel> listData, Context context) {
    this.listData = listData;
    this.context = context;
}

@Override
public CartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View itemView = inflater.inflate(R.layout.cart_layout, parent, false);
    return new CartViewHolder(itemView);
}

@Override
public void onBindViewHolder(CartViewHolder holder, int position) {
   /* TextDrawable drawable = TextDrawable.builder().buildRound("" + listData.get(position).getQuantity(), Color.RED);
    holder.img_cart_count.setImageDrawable(drawable);
 */
    String price = (Integer.parseInt(String.valueOf(listData.get(position).getPret()))) + " RON" ; /* * (Integer.parseInt(listData.get(position).getQuantity())); */
    holder.txt_price.setText((price));
    holder.txt_cart_name.setText(listData.get(position).getDenumireProdus());

这是列表OrderModel.java,其中包含getter和setter:

 public class OrderModel {

private String DenumireProdus;
private String Cantitate;
private int Pret;


public OrderModel(String denumireProdus, String cantitate, int pret) {
    DenumireProdus = denumireProdus;
    Cantitate = cantitate;
    Pret = pret;
}

public OrderModel(String denumireProdus, String cantitate, String pret) {
}

public String getDenumireProdus() {
    return DenumireProdus;
}

public void setDenumireProdus(String denumireProdus) {
    DenumireProdus = denumireProdus;
}

public String getCantitate() {
    return Cantitate;
}

public void setCantitate(String cantitate) {
    Cantitate = cantitate;
}

public int getPret() {
    return Pret;
}

public void setPret(int pret) {
    Pret = pret;
}

我真的需要帮助,我已经死了,我不想重写所有程序。

我希望在数据库中写一些东西,但是根本不写任何东西。但是,当我单击该项目时,敬酒词表示恰好是项目,我认为在某个连接不好的地方。

database.class

  public class Database extends SQLiteAssetHelper {
private static String DB_NAME="OrderMeDB.db";
private static int DB_VER=1;

public Database(Context context) {
    super(context, DB_NAME,null,DB_VER );
}

public List<OrderModel> getCarts()
{
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String[] sqlSelect = {"DenumireProdus" , "Cantitate" , "Pret"};
    String sqlTable = "Comanda";

    qb.setTables(sqlTable);
    Cursor c = qb.query(db , sqlSelect , null,null,null,null,null);

    final List<OrderModel> result = new ArrayList<>();
    if(c.moveToFirst())
    {
        do{
            result.add(new OrderModel(c.getString(c.getColumnIndex("DenumireProdus")),
                    c.getString(c.getColumnIndex("Cantitate")),
                    c.getString(c.getColumnIndex("Pret"))
            ));
        }while (c.moveToNext());
    }
    return result;
}

public void addToCart(OrderModel order)
{
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("INSERT INTO Comanda(DenumireProdus,Cantitate,Pret) VALUES('%s','%s','%s');",
            order.getDenumireProdus(),
            order.getCantitate(),
            order.getPret());
    db.execSQL(query);
}

1 个答案:

答案 0 :(得分:0)

终于明白了!您正在尝试写入数据,但使用的是getReadableDatabase()方法。尝试使用https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getWritableDatabase()

编辑:实际上可能是构造函数问题!