0

In my application I have a booking system which allows users to book tee times for specific times during the day. When a booking has been completed the details are saved to my Firebase and the user can then close the alert dialog. When the alert dialog is then closed the button which was clicked is then made unusable. Problem is that when the user leaves the booking activity and comes back the button is then useable, and if a different user then accesses the page the button is also able to be clicked as well. How do I solve this problem? Should I be saving the UID of the user in the 9am child ?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_booking);
    findViewById(R.id.profilebtn).setOnClickListener(this);
    findViewById(R.id.booking9am).setOnClickListener(this);
    book9am = (Button)findViewById(R.id.booking9am);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.profilebtn:
            finish();
            startActivity(new Intent(Booking.this, ProfileActivity.class));
            break;
        case R.id.booking9am:
            final AlertDialog.Builder mBuilder = new AlertDialog.Builder(Booking.this);
            View mView = getLayoutInflater().inflate(R.layout.dialog_booking,null);
            final EditText mPlayer1 = (EditText) mView.findViewById(R.id.player1);
            final EditText mPlayer2= (EditText) mView.findViewById(R.id.player2);
            final EditText mPlayer3 = (EditText) mView.findViewById(R.id.player3);
            final EditText mPlayer4 = (EditText) mView.findViewById(R.id.player4);
            final EditText mTime = (EditText) mView.findViewById(R.id.timeedit);
            final Button mBookingbtn = (Button) mView.findViewById(R.id.bookingbtn);
            mBookingbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String player1= mPlayer1.getText().toString().trim();
                    String player2= mPlayer2.getText().toString().trim();
                    String player4= mPlayer4.getText().toString().trim();
                    String player3= mPlayer3.getText().toString().trim();
                    if (player1.isEmpty()) {
                        mPlayer1.setError("Please enter player 1");
                        mPlayer1.requestFocus();
                        return;
                    }
                    if (player2.isEmpty()) {
                        mPlayer2.setError("Please enter player 2");
                        mPlayer2.requestFocus();
                        return;
                    }
                    if (player3.isEmpty()) {
                        mPlayer3.setError("Please enter player 2");
                        mPlayer3.requestFocus();
                        return;
                    }if (player2.isEmpty()) {
                        mPlayer4.setError("Please enter player 2");
                        mPlayer4.requestFocus();
                        return;
                    }
                    String playerone = mPlayer1.getText().toString();
                    String playertwo = mPlayer2.getText().toString();
                    String playerthree = mPlayer3.getText().toString();
                    String playerfour = mPlayer4.getText().toString();
                    String teetime= mTime.getText().toString().trim();
                    DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Booking").child("9am");
                    Map newPost = new HashMap();
                    newPost.put("playerone",playerone);
                    newPost.put("playertwo",playertwo);
                    newPost.put("playerthree",playerthree);
                    newPost.put("playerfour",playerfour);
                    newPost.put("teetime",teetime);
                    current_user_db.setValue(newPost);
                    Toast.makeText(Booking.this, "Booking Confirmed", Toast.LENGTH_SHORT).show();
                    book9am.setClickable(false);


                }


            });
            mBuilder.setNeutralButton("Close ", new DialogInterface.OnClickListener() { // define the 'Cancel' button
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            mBuilder.setView(mView);
            AlertDialog dialog = mBuilder.create();
            dialog.show();


    }

}
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Aaron Nicholl
  • 121
  • 2
  • 9
  • Well, do make it `unclickable` you can call `setEnabled(false)` but about the problem with going back to activity, I think this has to be handled on server part – Ionut J. Bejan Mar 30 '18 at 12:45
  • i would do it a little different. instead of making the button unclicked, hide the button, display a textview, and write booked. or whatever. if you are saving it into firebase, when you need to get the ressults from firebase PRIOR to displaying the activity. a lot of good answers below to choose from – letsCode Mar 30 '18 at 12:50

5 Answers5

1

In your onCreate method -

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Booking").child("9am");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists())
        {
           book9am.setClickable(false);
        }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
Nirel
  • 1,855
  • 1
  • 15
  • 26
0

well there could be multiple approaches to this problem. One is to set a Boolean variable in local storage Shared preference against every user.... Once your click the button set the value to true and when u come back in app check if variable is true then disable button..

Second solution Store the varible against every user on firebase and check(recommended since user can change phone)

0

Before showing the activity you will have to make a request to your firebase to check if the booking has been completed and depending on the result make the button enabled or not.

Dmitry
  • 2,326
  • 1
  • 13
  • 18
0

There are two approaches to your problem, depending on your needs.


First, is saving the button's state locally (on the client side), which means that after removing and re-installing the app for example, the state will be reset as well.

In order to save the button's state "forever", you should save the wanted state on the device, and this is what SharedPreferences is made for. This is a good example of using it.

Here is how you should implement it in your code:

public static void set_isButtonClickable(Context ctx, Boolean bool) {
    SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
    editor.putBoolean("BUTTON_CLICKABLE_STATE", bool);
    editor.commit();
}

public static boolean getPrefIsFirstLaunch(Context ctx) {
    return getSharedPreferences(ctx).getBoolean("BUTTON_CLICKABLE_STATE",false);
}

Second, is saving the button's state on the server side. Removing and re-installing the app obviously won't change its state. Make each user a variable which called "button_state" and change it as needed:

enter image description here

Tal Barda
  • 4,067
  • 10
  • 28
  • 53
0

findViewById(R.id.booking9am).setOnClickListener(this) instead of this use:-

 book9am = (Button)findViewById(R.id.booking9am);
book9am.setOnClickListener(this);

and instead of book9am.setClickable(false) set book9am.setEnable(false);

                                 OR

If you want button disable on some conditions then it can be managed at server side also.

Ankita
  • 1,129
  • 1
  • 8
  • 15