WithdrawActivity (Users withdraw their Earnings)

WithdrawActivity.java






  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.ramesh.taskearnerexample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class WithdrawActivity extends AppCompatActivity {

    private Button btnWithdraw;
    private TextView tvTotalEarn, tvPayments, tvWithdraws;
    private FirebaseAuth firebaseAuth;
    private FirebaseDatabase firebaseDatabase;
    private int Minimum_Withdraw_Amount = 100;
    private UserProfile userrupees;
    private FirebaseUser firebaseUser;
    private int value = 100;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_withdraw );

        btnWithdraw = findViewById(R.id.btnWithdraw);
        tvTotalEarn = findViewById(R.id.tvTotalEarn);
        tvPayments = findViewById( R.id.tvPayments );
        tvWithdraws = findViewById( R.id.tvWithdraws );


        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();




        firebaseAuth = FirebaseAuth.getInstance();
        firebaseDatabase = FirebaseDatabase.getInstance();






        DatabaseReference databaseReference = firebaseDatabase.getReference(firebaseAuth.getUid());


        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                final UserProfile userProfile = dataSnapshot.getValue(UserProfile.class);
                tvTotalEarn.setText( "My Total Earned Money: " + userProfile.getUserRupees() );
                tvPayments.setText( "No.of Successful Payments: " + userProfile.getuserPayments() );
                tvWithdraws.setText( "No.of Successful Withdrawls: " + userProfile.getUserWithdraw() );

                btnWithdraw.setOnClickListener( new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                            if (userProfile.userRupees >= 100) {

                                firebaseAuth = FirebaseAuth.getInstance();
                                firebaseDatabase = FirebaseDatabase.getInstance();

                                FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
                                final DatabaseReference myRef = firebaseDatabase.getReference( firebaseAuth.getUid() );

                                myRef.addListenerForSingleValueEvent( new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        long value = (long) dataSnapshot.child( "userWithdraw" ).getValue();
                                        System.out.println( "count before setValue()=" + value );
                                        myRef.child( "userWithdraw" ).setValue( ++value );  // <= Change to ++count
                                        System.out.println( "count after setValue()=" + value );
                                        myRef.child( "userRupees" ).setValue( userProfile.userRupees - 100 );


                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {
                                        // throw an error if setValue() is rejected
                                        throw databaseError.toException();
                                    }
                                } );

                            } else {
                                Toast.makeText( WithdrawActivity.this, "You must need minimum Rs.100", Toast.LENGTH_SHORT ).show();
                            }
                        }

                } );



            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(WithdrawActivity.this, databaseError.getCode(), Toast.LENGTH_SHORT).show();
            }
        });



    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case android.R.id.home:
                onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }


}
Powered by Blogger.