Haritha Madushanka
Haritha Madushanka

Reputation: 39

How to retrieve a Boolean value from Firebase to a TextView?

I'm making a shopping app and User has to subscribe to the 'Fast-delivery' option if he needs to order it faster. When the User puts a tick to the 'Fast-delivery' Checkbox, the boolean value is being uploaded to the Firebase realtime database --> database

And I want to see if the User has subscribed to the 'Fast-delivery' option from the Admin Panel. I retrieve all the order information to a RecyclerView in the Admin Panel. I want to set a TextView as "Fast Delivery : On/Off" when the Admin views the order details from the Admin Panel.

This is what I have tried:

@Override
protected void onStart()
{
    super.onStart();

    ordersRef = FirebaseDatabase.getInstance().getReference().child("Orders");

    FirebaseRecyclerOptions<AdminOrders> options =
            new FirebaseRecyclerOptions.Builder<AdminOrders>()
            .setQuery(ordersRef, AdminOrders.class)
            .build();

    FirebaseRecyclerAdapter<AdminOrders, AdminOrdersViewHolder> adapter =
            new FirebaseRecyclerAdapter<AdminOrders, AdminOrdersViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull final AdminOrdersViewHolder holder, final int position, final @NonNull AdminOrders model)
                {
                    holder.userName.setText("Name: "+ model.getName());
                    holder.userPhoneNumber.setText("Phone Number: : "+ model.getPhone());
                    holder.userTotalPrice.setText("Total Amount: $"+ model.getTotalAmount());
                    holder.userDateTime.setText("Order Time: "+ model.getDate() + model.getTime());
                    holder.userShippingAddress.setText("Shipping Address: "+ model.getAddress() + "," + model.getCity());
                    holder.showOrdersBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v)
                        {
                            String uID = getRef(position).getKey();
                            Intent intent = new Intent(AdminNewOrdersActivity.this, AdminUserProductsActivity.class);
                            intent.putExtra("uid", uID);
                            startActivity(intent);
                        }
                    });


                    ordersRef.child(userID).child("fast_delivery").addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                            TextView switch1 = (TextView) findViewById(R.id.switch1);
                            switch1.setText(String.valueOf(this));
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });
                }

And I'm getting this error

java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()

Upvotes: 2

Views: 517

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138979

You have a NullPointerException indicating that userID is not initialized, hence this error:

Can't pass null for argument 'pathString' in child()

To solve this, please add the following line of code:

String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();

Right in front of:

ordersRef.child(userID).child("fast_delivery").addValueEventListener(/* ... */);

Upvotes: 2

Black mamba
Black mamba

Reputation: 472

i guess if im right.. you have created separate tree for orders where you are getting orders

create model class give name as same as childname are where you're getting boolean value of fast_delivery use that boolean value to convert it to string and hence compare that value if String.valueof(boolean)is="true" then print to textview or vise versa with off

Upvotes: 0

Related Questions