在我的应用程序中,我有用户和产品实体,因此我将所有产品存储在类别中。当用户添加新产品时,我从该产品获取ID并将该ID存储在用户实体中。
通过这种方式,我知道每个用户他/她添加了哪些产品。我正在从数据库中检索数据并将其显示在我的RecyclerView
中。
问题是:
我不知道我的方式是否是从两个实体获取数据的好方法,还有其他方法可以更轻松地搜索where
SQL
中的"User": {
"email" : {
"email" : "email",
"hasLoggedInWithPassword" : false,
"name" : "mohammad tofi",
"products" : {
"-KR5LcHGurDceOO5xP_a" : {
"categoryName" : "OtherBooks",
"key" : "-KR5LcGdnNfQ-L4NWmOp"
}
}
}
}
关键字。
当我从产品实体检索数据时,我得到的第二件事就是异常:
com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为type.Product
用户实体中的数据:
"Products" :{
"otherbooks" : {
"-KR5LcGdnNfQ-L4NWmOp" : {
"brand" : "Albania",
"category" : "OtherBooks",
"condition" : "New with Tags"
}
}
}
产品实体中的数据:
private void initRecyclerView(View root) {
final RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view_user_product);
if (recyclerView != null) {
recyclerView.setHasFixedSize(true);
}
listViewProduct = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(listViewProduct);
findAttrOfProductUser();//**step two get all products in User Data**
recyclerView.setAdapter(adapter);
}
private void findAttrOfProductUser() {
//here I get the key and category name for each product in user entity and send to getProductUser.
getProductUser(value,value2);//****
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
private void getProductUser(String categoryName, String key) {
DatabaseReference devs = FirebaseDatabase.getInstance().getReference().child("Products").child(categoryName).child(key);
adapter = new FirebaseRecyclerAdapter<Product, userProductViewHolder>(Product.class, R.layout.user_product_list, userProductViewHolder.class, devs) {
@Override
protected Product parseSnapshot(com.google.firebase.database.DataSnapshot snapshot) {
Product productuser = snapshot.getValue(Product.class);//Exception
return productuser;
}
@Override
protected void populateViewHolder(userProductViewHolder viewHolder, final Product model, int position) {
viewHolder.productName.setText(model.getTitle());
viewHolder.productPrice.setText(model.getSellPrice() + "");
Picasso.with(getActivity()).load(model.getImageuri0()).resize(0, 200).into(viewHolder.productImage);
// products.add(model);
}
};
}
public class Product{
private String brand;
private String category;
private String condition;
public Product(String brand, String category, String condition){
this.brand = brand;
this.category = category;
this.condition = condition;
}
//then I make get and set for this attribute and empty constructor.
}
我的代码:
link: function(scope, element, attrs, ctrl) {
element[0].addEventListener('click', function() {
ctrl.checkSelected();
})
}
提前致谢。