enter image description here I am working on android using Firebase Database. I want to retrieve the data on a listview of firebase database. I am providing the code and picture of which data have to retrieve.
Model Class:
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;
}
}
Adapter Class:
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;
}
}
Main Class: public class RiceCategory extends 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) {
}
});
}
I am really stuck in it I tried different methods but I didn't get any result so I posted here to know from you guys if you find any mistake in the code so please post on comments and make it correct. I'll be very thankful to you