Muhammad Arsalan
Muhammad Arsalan

Reputation: 31

not be able to retrieve firebase data using addValueEventListener

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

Output the data

I this is firebase databse snapshot

firebase datasbe snapshot

updated firebase databse snapshot

Updated output

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();
        }

Upvotes: 0

Views: 281

Answers (2)

Peter Haddad
Peter Haddad

Reputation: 80914

I think its nested too much but to answer your question, you need to remove this from your code:

  dealList.clear();

and add it outside of the addValueEventListener()

like this:

protected void onStart() {
super.onStart();
   dealList.clear();
rootRef.addValueEventListener(new ValueEventListener() {

Edit:

  @Override
 protected void onStart() {
super.onStart();
  dealList.clear();
rootRef.child(expertId).child("Cooker Deals").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);
            dealList.add(info);



        }

        DealList adapter = new DealList( New_Deal_List.this,dealList);
        lvDealList.setAdapter(adapter);
        adapter.notifydatasetchanged();
    }

Upvotes: 1

Alex Mamo
Alex Mamo

Reputation: 138824

Assuming that faum-expert nide is a direct child of your Firebase database, to get that data, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("faum-expert").child("Expert").child(expertId).child("Cooker Deals");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String dealName = ds.child("Deals Information").child("dealName").getValue(String.class);
            String newDealCategory = ds.child("Deals Information").child("newDealCategory").getValue(String.class);
            Log.d("TAG", dealName + " / " + newDealCategory);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(eventListener);

The output will be:

4567 / kagj

But pay atention, the fields inside your model class are first letter uppercase and in your database are first letter lowercase. To solve this, add the following annotations in front of your fields like this:

@PropertyName("dealName")
private String DealName;
@PropertyName("newDealCategory")
private String NewDealCategory;
@PropertyName("dishName")
private String DishName;
@PropertyName("dealDescription")
private String DealDescription;

Upvotes: 0

Related Questions