Muhammad Arsalan
Muhammad Arsalan

Reputation: 31

Retrieve selected data from firebase

I want to get selected data from firebase. Actually I used listview for getting values. On selected item I need more info from data. But unfortunately am not be able to get selected data. That's relevant code.

New_Deal_List.java

lvDealList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            NewDeal_Database newDeal_database = dealList.get(i);

            Intent intent = new Intent(getApplicationContext(), Deal_Detail_one.class);

            mEditor.putString(getString(R.string.COOKER_ID), newDeal_database.getuId());
            mEditor.commit();
            passingtoDealDaysCheckValues(newDeal_database.getDealId());


            startActivity(intent);
        }
    });


    }

Passing deal id to this function.

    dealDaysRefrence.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot dealSnapshot : dataSnapshot.getChildren())
            {
                NewDeal_Database newDeal_database = dealSnapshot.child(DealId).getValue(NewDeal_Database.class);

                try{
                    Toast.makeText(New_Deal_List.this,dealSnapshot.child(DealId).getValue(NewDeal_Database.class).toString(),Toast.LENGTH_SHORT).show();
                    if(newDeal_database.getMonday()== true ){
                        Toast.makeText(getApplicationContext(),DEAL_DAYS_MONDAY,Toast.LENGTH_SHORT).show();
                        DEAL_DAYS_MONDAY = "Available";
                    }else{
                        Toast.makeText(getApplicationContext(),DEAL_DAYS_MONDAY,Toast.LENGTH_SHORT).show();
                        DEAL_DAYS_MONDAY = "Not-Available";
                    }
                    if(dealSnapshot.child(DealId).getValue(NewDeal_Database.class).getTuesday() == true){
                        DEAL_DAYS_TUESDAY = "Available";
                        Toast.makeText(getApplicationContext(),DEAL_DAYS_TUESDAY,Toast.LENGTH_SHORT).show();

                    }else{
                        DEAL_DAYS_TUESDAY = "Not-Available";
                        Toast.makeText(getApplicationContext(),DEAL_DAYS_TUESDAY,Toast.LENGTH_SHORT).show();
                    }
                    if(dealSnapshot.child(DealId).getValue(NewDeal_Database.class).getWednesday() == true){
                        DEAL_DAYS_WEDNESDAY = "Available";
                    }else{
                        DEAL_DAYS_WEDNESDAY = "Not-Available";
                    }
                    if(dealSnapshot.child(DealId).getValue(NewDeal_Database.class).getThursday() == true){
                        DEAL_DAYS_THURSDAY = "Available";
                    }else{
                        DEAL_DAYS_THURSDAY = "Not-Available";
                    }
                    if(dealSnapshot.child(DealId).getValue(NewDeal_Database.class).getFriday() == true){
                        DEAL_DAYS_FRIDAY = "Available";
                    }else{
                        DEAL_DAYS_FRIDAY = "Not-Available";
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }finally {

                    mEditor.putString(getString(R.string.DEAL_DAYS_MONDAY), DEAL_DAYS_MONDAY);
                    mEditor.commit();

                    mEditor.putString(getString(R.string.DEAL_DAYS_TUESDAY), DEAL_DAYS_TUESDAY);
                    mEditor.commit();

                    mEditor.putString(getString(R.string.DEAL_DAYS_WEDNESDAY), DEAL_DAYS_WEDNESDAY);
                    mEditor.commit();

                    mEditor.putString(getString(R.string.DEAL_DAYS_THURSDAY), DEAL_DAYS_THURSDAY);
                    mEditor.commit();

                    mEditor.putString(getString(R.string.DEAL_DAYS_FRIDAY), DEAL_DAYS_THURSDAY);
                    mEditor.commit();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

In above code, if and else condition not getting desired values.

Also android studio give me suggetion. Check suggetion snapshot

This is NewDeal_Database.java class, where i set the values.

public NewDeal_Database(Boolean Monday, Boolean Tuesday, Boolean Wednesday, Boolean Thursday, Boolean Friday){
    this.Monday = Monday;
    this.Tuesday = Tuesday;
    this.Wednesday = Wednesday;
    this.Thursday = Thursday;
    this.Friday = Friday;





public Boolean getMonday() {
    return Monday;
}

public Boolean getTuesday() {
    return Tuesday;
}

public Boolean getWednesday() {
    return Wednesday;
}

public Boolean getThursday() {
    return Thursday;
}

public Boolean getFriday() {
    return Friday;

}

I am inserting data from into firebase in New_Deal_Time.java using this codeNew_Deal_Time.java dealDaysRefrence.child(id).child(DealId).setValue(dealdays);

firebase snapshot

enter image description here

suggsetion snapshot

enter image description here

Upvotes: 0

Views: 783

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138834

First, your NewDeal_Database.java class should look like this:

public class NewDeal_Database {
    public NewDeal_Database(Boolean monday, Boolean tuesday, Boolean wednesday, Boolean thursday, Boolean friday){
        this.monday = monday;
        this.tuesday = tuesday;
        this.wednesday = wednesday;
        this.thursday = thursday;
        this.friday = friday;


    public NewDeal_Database {} //Needed for Firebase


    public Boolean getMonday() {
        return monday;
    }

    public Boolean getTuesday() {
        return tuesday;
    }

    public Boolean getWednesday() {
        return wednesday;
    }

    public Boolean getThursday() {
        return thursday;
    }

    public Boolean getFriday() {
        return friday;
    }
}

To get that data, please use the following code:

ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String eightToten = ds.child("eightToten").getValue(Boolean.class);
            String nineToeleven = ds.child("nineToeleven").getValue(Boolean.class);
            String sixToeight = ds.child("sixToeight").getValue(Boolean.class);
            String twelveTotwo = ds.child("twelveTotwo").getValue(Boolean.class);
            Log.d("TAG", eightToten + " / " + nineToeleven + " / " + sixToeight + " / " + twelveTotwo);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
dealDaysRefrence.child(id).addListenerForSingleValueEvent(eventListener);

Upvotes: 1

Related Questions