【发布时间】:2019-12-09 19:58:14
【问题描述】:
我正在我的项目中使用 dbSqlite 制作购物车。我可以从 sqlite 添加和检索数据。我也可以添加到recyclierview。但是,我在 recyclierview 上创建了一个产品列表并从中获取数据。单击购物车按钮时,它应该加载到 recyclierview。如何在不手动添加列表的情况下将所有数据从 sqlite 加载到 recyclierview?
我可以像在 CartFragment.java 中那样将数据从数据库添加到 recyclerview:
db = new DatabaseHelper(getContext());
Cursor rs = db.getData();
rs.moveToFirst();
img = rs.getString(rs.getColumnIndex("Image"));
title = rs.getString(rs.getColumnIndex("Title"));
cost = rs.getString(rs.getColumnIndex("Cost"));
market = rs.getString(rs.getColumnIndex("Market"));
productList = new ArrayList<>();
product = new Product(title, img, cost,"asdas",market,"");
productList.add(product);
product = new Product(getString(R.string.dell_title), getString(R.string.dell_url), "11.990 TL","dasdaaaaas","Migros","");
productList.add(product);
CartAdapter.java
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> {
private Context mContext;
private List<Product> productList;
private TextView textMarketName, textProductName, textProductCost, textProfit;
private ImageView imgProduct;
public CartAdapter(Context mContext, List<Product> productList) {
this.mContext = mContext;
this.productList = productList;
}
@NonNull
@Override
public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_item_view, parent, false);
return new CartViewHolder(view);
}
@Override
public void onBindViewHolder(CartViewHolder cartViewHolder, int position) {
Glide.with(mContext).load(productList.get(position).getProductImage())
.apply(RequestOptions.placeholderOf(R.drawable.ic_glide_img).error(R.drawable.ic_glide_warning)).
into(imgProduct);
textProductName.setText(productList.get(position).getProductName());
textMarketName.setText(productList.get(position).getProductMarket());
textProductCost.setText(productList.get(position).getProductCost());
}
@Override
public int getItemCount() {
return productList.size();
}
class CartViewHolder extends RecyclerView.ViewHolder {
CartViewHolder(@NonNull View itemView) {
super(itemView);
imgProduct = itemView.findViewById(R.id.imageView_cart_product);
textMarketName = itemView.findViewById(R.id.textViewCart_MarketName);
textProductName = itemView.findViewById(R.id.textView_cart_product_title);
textProductCost = itemView.findViewById(R.id.textView_cart_product_cost);
textProfit = itemView.findViewById(R.id.textView_cart_profit);
}
}
}
Product.java
public class Product {
private String productName;
private String productImage;
private String productCost;
private String productDescription;
private String productMarket;
private String productMarketImage;
public Product(){
}
public Product(String productName, String productImage, String productCost, String productDescription, String productMarket, String productMarketImage) {
this.productName = productName;
this.productImage = productImage;
this.productCost = productCost;
this.productDescription = productDescription;
this.productMarket = productMarket;
this.productMarketImage = productMarketImage;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductImage() {
return productImage;
}
public void setProductImage(String productImage) {
this.productImage = productImage;
}
public String getProductCost() {
return productCost;
}
public void setProductCost(String productCost) {
this.productCost = productCost;
}
public String getProductDescription() {
return productDescription;
}
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
public String getProductMarket() {
return productMarket;
}
public void setProductMarket(String productMarket) {
this.productMarket = productMarket;
}
public String getProductMarketImage() {
return productMarketImage;
}
public void setProductMarketImage(String productMarketImage) {
this.productMarketImage = productMarketImage;
}
}
CartFragment.java
public class CartFragment extends Fragment {
private ImageView imageShare;
List<Product> productList;
List<Supermarket> supermarketList;
Product product;
Supermarket supermarket;
DatabaseHelper db;
String title, cost, img, market;
private Toolbar toolbar;
private RecyclerView recyclerViewCart;
private OnFragmentInteractionListener mListener;
public CartFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_cart, container, false);
recyclerViewCart = v.findViewById(R.id.recyclerview_cart);
LinearLayoutManager verticalLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerViewCart.setLayoutManager(verticalLayoutManager);
db = new DatabaseHelper(getContext());
Cursor rs = db.getData();
rs.moveToFirst();
img = rs.getString(rs.getColumnIndex("Image"));
title = rs.getString(rs.getColumnIndex("Title"));
cost = rs.getString(rs.getColumnIndex("Cost"));
market = rs.getString(rs.getColumnIndex("Market"));
productList = new ArrayList<>();
product = new Product(title, img, cost,"asdas",market,"");
productList.add(product);
CartAdapter myAdapter = new CartAdapter(getActivity(), productList);
recyclerViewCart.setAdapter(myAdapter);
}
【问题讨论】:
-
你想做什么?
-
扩展CursorRecyclerAdapter适配器而不是
RecyclerView.Adapter -
@PembaTamang 我想在单击购物车按钮时将购物车作为列表添加到 recyclierview。
标签: java android sqlite android-recyclerview android-sqlite