[英]Unable to fetch data from database SQLite in android
在这里,我正在尝试开发一个电子商务应用程序。 我在数据库插入中添加了一些产品细节很好,但是当我尝试从数据库中获取数据以显示在主页上时,它显示的是一个空的 arraylist。
数据库助手 class
package com.food.handtohand.databases;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.food.handtohand.MainActivity;
import com.food.handtohand.modal.AddProductModel;
import com.food.handtohand.modal.productmodal;
import java.util.ArrayList;
import java.util.List;
public class AddProductDatabase extends SQLiteOpenHelper {
public AddProductDatabase(@Nullable Context context) {
super(context, "products.db", null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTableStatement = "CREATE TABLE products (id INTEGER PRIMARY KEY
AUTOINCREMENT , image1 BLOB , image2 BLOB , image3 BLOB , productName TEXT ,productPrice
INTEGER , unit TEXT, productDescription TEXT) ";
db.execSQL(createTableStatement);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public boolean addProduct (ArrayList<byte[]> img, String name , int price , String unit
, String description ){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("image1" , img.get(0));
cv.put("image2" ,img.get(1) );
cv.put("image3" , img.get(2));
cv.put("productName" , name);
cv.put("productPrice" , price);
cv.put ("productDescription" , description);
cv.put("unit" , unit);
long products = db.insert("products" , null , cv);
db.close();
if(products == -1){
return false;
}else {
return true;
}
}
public ArrayList<productmodal> getAll() {
ArrayList<productmodal> array = new ArrayList<>();
String querySelect = "SELECT image1,productName,productPrice FROM products";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursorSelect = db.rawQuery(querySelect,null);
if (cursorSelect.moveToFirst()) {
do {
byte[] image = cursorSelect.getBlob(1);
String name = cursorSelect.getString(4);
int price = cursorSelect.getInt(5);
productmodal model = new productmodal(image, name, price);
array.add(model);
}while(cursorSelect.moveToNext());
}else{
}
cursorSelect.close();
db.close();
return array;
}
}
MainActivity 回收器查看代码
productRecycler = findViewById(R.id.productrecycler);
ArrayList<productmodal> list2 = AddProductDB.getAll();
if(list2.size() >0) {
productAdapter adapter2 = new productAdapter(list2, this);
productRecycler.setAdapter(adapter2);
GridLayoutManager grid = new GridLayoutManager(this, 2);
productRecycler.setLayoutManager(grid);
}else{
Toast.makeText(MainActivity.this,"No product Addeddd" ,
Toast.LENGTH_LONG).show();
}
Modal class
package com.food.handtohand.modal;
public class productmodal {
byte[] productImage;
String productName;
int productPrice;
public productmodal(byte[] productImage, String productName, int productPrice) {
this.productImage = productImage;
this.productName = productName;
this.productPrice = productPrice;
}
public byte[] getProductImage() {
return productImage;
}
public void setProductImage(byte[] productImage) {
this.productImage = productImage;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getProductPrice() {
return productPrice;
}
public void setProductPrice(int productPrice) {
this.productPrice = productPrice;
}
}
适配器 class
package com.food.handtohand.adapter;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.food.handtohand.MainActivity;
import com.food.handtohand.ProductDetail;
import com.food.handtohand.R;
import com.food.handtohand.modal.productmodal;
import java.util.ArrayList;
import java.util.List;
public class productAdapter extends RecyclerView.Adapter<productAdapter.viewholder>{
ArrayList<productmodal> list;
Context context;
public productAdapter(ArrayList list , MainActivity context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.products , parent ,
false);
return new viewholder(view);
}
@Override
public void onBindViewHolder(@NonNull viewholder holder, int position) {
productmodal products = list.get(position);
byte[] imgbyte = products.getProductImage();
Bitmap imagebytes = BitmapFactory.decodeByteArray(imgbyte, 0, imgbyte.length);
holder.productImage.setImageBitmap(imagebytes);
holder.price.setText(products.getProductPrice());
holder.name.setText(products.getProductName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, ProductDetail.class);
context.startActivity(intent);
}
} );
}
@Override
public int getItemCount() {
return list.size();
}
public class viewholder extends RecyclerView.ViewHolder {
ImageView productImage;
TextView name , price;
public viewholder(@NonNull View itemView) {
super(itemView);
productImage = itemView.findViewById(R.id.productimg);
name = itemView.findViewById(R.id.productname);
price = itemView.findViewById(R.id.productprice);
}
}
}
xml 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
app:layoutDescription="@xml/activity_main_scene"
tools:context=".MainActivity" >
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_anchor="@+id/constraintLayout"
app:layout_anchorGravity="center">
<include
android:id="@+id/toolbaar"
layout="@layout/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbaar">
</androidx.recyclerview.widget.RecyclerView>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/recycle">
<SearchView
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@drawable/searchback"
android:clickable="true"
android:elevation="5dp"
android:focusable="true"
android:iconifiedByDefault="false"
android:keepScreenOn="false"
android:padding="2dp"
android:queryHint="Apko kya chahiye ?"
android:requiresFadingEdge="vertical"
android:soundEffectsEnabled="true">
</SearchView>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/productrecycler"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/include"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout2" />
<include
android:id="@+id/include"
layout="@layout/bottomnav"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
请告诉故障在哪里
您的查询:
SELECT image1, productName, productPrice FROM products
仅返回表所有行的 3 列。
cursor object 中每一列的索引都是从 0 开始的,这意味着列image1
的索引是0
, productName
的索引是1
, productPrice
的索引是2
。
因此,在do
循环中,您应该更改为:
do {
byte[] image = cursorSelect.getBlob(0);
String name = cursorSelect.getString(1);
int price = cursorSelect.getInt(2);
productmodal model = new productmodal(image, name, price);
array.add(model);
} while(cursorSelect.moveToNext());
另一种方法是使用getColumnIndex()
方法,这样您就不需要依赖列的索引:
int ind_image = cursorSelect.getColumnIndex("image1");
int ind_productName = cursorSelect.getColumnIndex("productName");
int ind_productPrice = cursorSelect.getColumnIndex("productPrice");
do {
byte[] image = cursorSelect.getBlob(ind_image);
String name = cursorSelect.getString(ind_productName);
int price = cursorSelect.getInt(ind_productPrice);
productmodal model = new productmodal(image, name, price);
array.add(model);
} while(cursorSelect.moveToNext());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.