I am trying to retrieve firebase data using addValueEventListener, but unfortunately i am not be able to get right data. I have New_Deal_List.java class, and in this class i want to retrieve `public class New_Deal_List extends AppCompatActivity {
ListView lvDealList; List<NewDeal_Database> dealList; DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("Expert"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.new__deal__list); lvDealList = (ListView)findViewById(R.id.lvDealList); dealList = new ArrayList<>(); } @Override protected void onStart() { super.onStart(); rootRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { dealList.clear(); for(DataSnapshot dealSnapshot : dataSnapshot.getChildren()){ NewDeal_Database info = dealSnapshot.getValue(NewDeal_Database.class); dealList.add(info); } DealList adapter = new DealList( New_Deal_List.this,dealList); lvDealList.setAdapter(adapter); } @Override public void onCancelled(DatabaseError databaseError) { Toast.makeText(New_Deal_List.this,"Databse error",Toast.LENGTH_SHORT).show(); } }); } I am adding data through New_Deal.java by this method
private void AddNewDeal(){ int DealName = Integer.parseInt(etDealName.getText().toString()); String NewDealCategory = etNewDealCategory.getText().toString(); String DishName = etDishName.getText().toString(); String DealDescription = etDealDescription.getText().toString(); if(TextUtils.isEmpty(etDishName.getText().toString()) || TextUtils.isEmpty(etNewDealCategory.getText().toString()) || TextUtils.isEmpty(etDishName.getText().toString()) || TextUtils.isEmpty(etDealDescription.getText().toString())){ Toast.makeText(this,"All fileds must be filled.",Toast.LENGTH_SHORT).show(); }else{ DealId = keyRefrence.push().getKey(); //firebaseDatabase data = new firebaseDatabase(DealName,NewDealCategory,DishName,DealDescription); //Contact_Info info = new Contact_Info( DealName, NewDealCategory, DealDescription); NewDeal_Database info = new NewDeal_Database(DealName,NewDealCategory, DishName, DealDescription); keyRefrence.child(Cooker_Deal).child(DealId).child(Deal).setValue(info); Toast.makeText(this,"Information Added",Toast.LENGTH_SHORT).show(); Intent intent = new Intent(New_Deal.this,New_Deal_Time.class); startActivity(intent); } } I am setting value using this NewDeal_Databse.java
public NewDeal_Database(int DealName,String NewDealCategory, String DishName, String DealDescription){ this.DealName = DealName; this.NewDealCategory = NewDealCategory; this.DishName = DishName; this.DealDescription = DealDescription; } public int getDealName() { return DealName; } public String getNewDealCategory() { return NewDealCategory; } public String getDishName() { return DishName; } public String getDealDescription() { return DealDescription; } Also i DealList.java for array adapter
public class DealList extends ArrayAdapter <NewDeal_Database> { private Activity context; private List<NewDeal_Database> dealList; public DealList(Activity context, List<NewDeal_Database> dealList){ super(context, R.layout.list_layout, dealList); this.context = context; this.dealList = dealList; } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { LayoutInflater inflater = context.getLayoutInflater(); View listViewItem = inflater.inflate(R.layout.list_layout,null,true); TextView tvDealName = (TextView)listViewItem.findViewById(R.id.tvDealNamelayout); TextView tvNewDealCategory = (TextView)listViewItem.findViewById(R.id.tvNewDealCategorylayout); NewDeal_Database info = dealList.get(position); tvDealName.setText(String.valueOf(info.getDealName())); tvNewDealCategory.setText(info.getNewDealCategory()); return listViewItem; } } I get these on values on output
I this is firebase databse snapshot
updated firebase databse snapshot
Problem solved using this code:
@Override protected void onStart() { super.onStart(); //dealList.clear(); rootRef.child(id).child(Cooker_Deal).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for(DataSnapshot dealSnapshot : dataSnapshot.getChildren()){ for(DataSnapshot datas : dealSnapshot.getChildren()){ // NewDeal_Database info = datas.getValue(NewDeal_Database.class); count++; if(count>3){ dealList.add(info); count=0; } } } DealList adapter = new DealList( New_Deal_List.this,dealList); lvDealList.setAdapter(adapter); adapter.notifyDataSetChanged(); }