【发布时间】:2019-01-12 08:29:15
【问题描述】:
enter image description here 我正在使用 Firebase 数据库开发 android。我想检索 firebase 数据库列表视图上的数据。我正在提供必须检索哪些数据的代码和图片。
模型类:
public class FoodItemclass {
private String ItemName;
private String price;
private int count;
public FoodItemclass()
{}
public FoodItemclass(String itemane,String itemprice,int con)
{
ItemName = itemane;
price =itemprice;
count = con;
}
public String getmItemName() {
return ItemName;
}
public void setmItemName(String mItemName) {
this.ItemName = mItemName;
}
public String getmItemPrice() {
return price;
}
public void setmItemPrice(String mItemPrice) {
this.price = mItemPrice;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
适配器类:
public class FoodItemAdapter extends ArrayAdapter<FoodItemclass> {
public FoodItemAdapter(Activity context, List<FoodItemclass> arrayList)
{
super(context,0,arrayList);
}
@Override
public View getView(int position,View convertView, ViewGroup parent) {
View ListItemView = convertView;
if(ListItemView == null)
{
ListItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
}
FoodItemclass foodItemclass = getItem(position);
TextView itename = ListItemView.findViewById(R.id.ItemnameId);
itename.setText(foodItemclass.getmItemName());
TextView itemprice = ListItemView.findViewById(R.id.ItemPriceId);
itemprice.setText(foodItemclass.getmItemPrice());
return ListItemView;
}
}
主类: 公共类 RiceCategory 扩展 AppCompatActivity {
private DatabaseReference databaseReference;
private FoodItemAdapter mFoodAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rice_category);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
databaseReference = FirebaseDatabase.getInstance().getReference("UpdatedMenu").child("Noodles & Rice");
final List<FoodItemclass> foodItemclasses = new ArrayList<>();
mFoodAdapter =new FoodItemAdapter(this,foodItemclasses);
ListView mListview = (ListView)findViewById(R.id.listview) ;
mListview.setAdapter(mFoodAdapter);
android.support.v7.app.ActionBar actionBar =getSupportActionBar();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
actionBar.setTitle("Rice");
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded( DataSnapshot dataSnapshot, String s) {
FoodItemclass food = dataSnapshot.getValue(FoodItemclass.class);
foodItemclasses.add(food);
mFoodAdapter.notifyDataSetChanged();
}
@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) {
}
});
}
我真的陷入了困境,我尝试了不同的方法,但没有得到任何结果,所以我在这里发布,如果你发现代码中有任何错误,请告诉你们,所以请在 cmets 上发布并改正。我会非常感谢你
【问题讨论】:
-
您在使用此代码时有什么问题?你有错误吗?
-
不,我无法在模型类中提到的文本视图上获取商品名称和价格的值:(
-
连你的价格都没有显示?你看到我从这个 post 的回答了吗?
-
我检查了兄弟,但是当我将价值价格放入列表时,它给了我错误,我不知道为什么
-
它给你一个错误是什么?
标签: android firebase firebase-realtime-database