这是我的数据库的屏幕截图,我已经尝试了很多来检索数据,但似乎总是返回null。我正在使用firebase数据库。我想知道如何将此数据检索到此布局中。顺便说一句,我是java和android的新手,所以在回答这个问题时请考虑一下。我遇到的另一个问题是如何在点击图像时传递图像的意图。我在家庭布局中使用多个图像视图,我不明白如何处理它。提前谢谢。
//从数据库中检索的产品图片 mDatabaseRef.child(" Products")。addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Product product = dataSnapshot.child("Product ID").getValue(Product.class);
product_1_name = dataSnapshot.child("Product 1").child("Product Name").getValue().toString();
product_1_price = dataSnapshot.child("Product 1").child("Product Price").getValue().toString();
product_1_urlimage = dataSnapshot.child("Product 1").child("Product Image").getValue().toString();
product_2_name = dataSnapshot.child("Product 2").child("Product Name").getValue().toString();
product_2_price = dataSnapshot.child("Product 2").child("Product Price").getValue().toString();
product_2_urlimage = dataSnapshot.child("Product 2").child("Product Image").getValue().toString();
product_3_name = dataSnapshot.child("Product 3").child("Product Name").getValue().toString();
product_3_price = dataSnapshot.child("Product 3").child("Product Price").getValue().toString();
product_3_urlimage = dataSnapshot.child("Product 3").child("Product Image").getValue().toString();
product_4_name = dataSnapshot.child("Product 4").child("Product Name").getValue().toString();
product_4_price = dataSnapshot.child("Product 4").child("Product Price").getValue().toString();
product_4_urlimage = dataSnapshot.child("Product 4").child("Product Image").getValue().toString();
product_5_name = dataSnapshot.child("Product 5").child("Product Name").getValue().toString();
product_5_price = dataSnapshot.child("Product 5").child("Product Price").getValue().toString();
product_5_urlimage = dataSnapshot.child("Product 5").child("Product Image").getValue().toString();
product_6_name = dataSnapshot.child("Product 6").child("Product Name").getValue().toString();
product_6_price = dataSnapshot.child("Product 6").child("Product Price").getValue().toString();
product_6_urlimage = dataSnapshot.child("Product 6").child("Product Image").getValue().toString();
product_7_name = dataSnapshot.child("Product 7").child("Product Name").getValue().toString();
product_7_price = dataSnapshot.child("Product 7").child("Product Price").getValue().toString();
product_7_urlimage = dataSnapshot.child("Product 7").child("Product Image").getValue().toString();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
private View.OnClickListener HomeImageClickListener = new View.OnClickListener(){ 意图;
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.iv_hotcakes_topleft:
intent = new Intent(getContext(), ProductDetailActivity.class);
intent.putExtra("title", product_1_name);
intent.putExtra("url", product_1_urlimage);
intent.putExtra("price", product_2_price);
startActivity(intent);
break;
case R.id.iv_hotcakes_bottomleft:
intent = new Intent(getContext(), ProductDetailActivity.class);
intent.putExtra("title", product_2_name);
intent.putExtra("url", product_2_urlimage);
intent.putExtra("price", product_2_price);
startActivity(intent);
break;
case R.id.iv_hotcakes_right:
intent = new Intent(getContext(), ProductDetailActivity.class);
intent.putExtra("title", product_3_name);
intent.putExtra("url", product_3_urlimage);
intent.putExtra("price", product_3_price);
startActivity(intent);
break;
case R.id.iv_specials_left:
intent = new Intent(getContext(), ProductDetailActivity.class);
intent.putExtra("title", product_4_name);
intent.putExtra("url", product_4_urlimage);
intent.putExtra("price", product_4_price);
startActivity(intent);
break;
case R.id.iv_specials_right:
intent = new Intent(getContext(), ProductDetailActivity.class);
intent.putExtra("title", product_5_name);
intent.putExtra("url", product_5_urlimage);
intent.putExtra("price", product_5_price);
startActivity(intent);
break;
case R.id.iv_categories_top_left:
intent = new Intent(getContext(), ProductDetailActivity.class);
intent.putExtra("title", product_6_name);
intent.putExtra("url", product_6_urlimage);
intent.putExtra("price", product_6_price);
startActivity(intent);
break;
case R.id.iv_categories_top_right:
intent = new Intent(getContext(), ProductDetailActivity.class);
intent.putExtra("title", product_7_name);
intent.putExtra("url", product_7_urlimage);
intent.putExtra("price", product_7_price);
startActivity(intent);
break;
case R.id.iv_categories_center_left:
intent = new Intent(getContext(), ProductDetailActivity.class);
intent.putExtra("title", "Muffins");
intent.putExtra("url", url);
intent.putExtra("price", "280");
startActivity(intent);
break;
case R.id.iv_categories_center_right:
intent = new Intent(getContext(), ProductDetailActivity.class);
intent.putExtra("title", "Muffins");
intent.putExtra("url", url);
intent.putExtra("price", "280");
startActivity(intent);
break;
case R.id.iv_categories_bottom_left:
intent = new Intent(getContext(), ProductDetailActivity.class);
intent.putExtra("title", "Muffins");
intent.putExtra("url", url);
intent.putExtra("price", "280");
startActivity(intent);
break;
case R.id.iv_categories_bottom_right:
intent = new Intent(getContext(), ProductDetailActivity.class);
intent.putExtra("title", "Muffins");
intent.putExtra("url", url);
intent.putExtra("price", "280");
startActivity(intent);
break;
}
}
};
答案 0 :(得分:0)
使用orderByValue()这将完成你的工作
mReference.child("Products").orderByValue().addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ProductsClass productsClass = dataSnapshot.getValue(ProductsClass.class);
if(productsClass!=null)
{
// do what ever you want
}else
Log.d(TAG, "onChildAdded: NULL");
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//这不是代码的一部分,而是如何使用hashmap
ArrayList<HashMap<String,String>> list = new ArrayList<>();
//HashMap<KEY,VALUE>
// just a example how to use hashmap in list
for(int i = 0;i<3;i++)
{
HashMap<String,String> map = new HashMap<>();
map.put("product_key"+i,"product_name"+i);
map.put("product_key_image"+i,"product_image_link"+i);
list.add(map);
}
// to get values
for(int i=0;i<list.size();i++)
{
HashMap<String,String> map = list.get(i);
System.out.println("product name is "+map.get("product_key"+i));
System.out.println("product name is "+map.get("product_key"+i));
}
//for single value
HashMap<String,String> map = new HashMap<>();
map.put("name", "salman");
map.put("full_name", "123123123");
System.out.println(map.get("name"));
System.out.println(map.get("full_name"));
<强>输出:强>
product name is product_name0
product name is product_name0
product name is product_name1
product name is product_name1
product name is product_name2
product name is product_name2
salman
123123123