Ad

How To Multiply The Value Of A Textview While The Button Is Incremented?

- 1 answer

I am currently on working on a project on firebase and I was wondering how to multiply the value while the button is incremented. For example, I have this image

Image

In this image, if I clicked the + button the Number of Orders will be 1 and it will keep increasing until I clicked the - button. On the bottom of it, there is the value which is 1.00. For every time the button is clicked for example it will become 1 it will also multiply the textview which is 1.00. Then if button is incremented it will be 2x1.00 which is 2.00.

This is the code I have done so far

public class Detailsoforder extends AppCompatActivity {

private static final String TAG = "AddToDatabase";
private static int click = 0;

private TextView titles;
private TextView increase;
private TextView Price;
private int count = 0;

//add Firebase
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;

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

    titles = findViewById(R.id.Order);
    increase = findViewById(R.id.Increase);
    Price = findViewById(R.id.Price);
    String title = getIntent().getStringExtra("title");
    String price = getIntent().getStringExtra("description");

    titles.setText(title);
    Price.setText(price);


    mAuth = FirebaseAuth.getInstance();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    myRef = mFirebaseDatabase.getReference();

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
        }
    };

    // Read from the database
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Object value = dataSnapshot.getValue();
            Log.d(TAG,"Value is"+value);
        }

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

        }
    });
}

public void onIncreaseClick(View view) {

    String price = Price.getText().toString();
    int result = Integer.parseInt(price);
    count++;
    increase.setText(String.valueOf(count));
    Price.setText(String.valueOf(count*result));


}
Ad

Answer

use this code

  Button plus = findViewById(R.id.plus);
  plus.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
       // for orders numbers
       int number = Integer.parseInt(ordersTextView.getText().toString().trim());
       number = number+1;
       ordersTextView.setText(String.valueOf(number));
      // for total cost 
       Float cost =  Float.parseFloat(costTextView.getText().toString().trim())*number;
       costTextView.setText(String.valueOf(cost));
    }
});
Ad
source: stackoverflow.com
Ad