I want a central way to control my buttons

Hi I have 2 lines each with a name textfield, 2 buttons and a textfield to show the amount. There will be more lines later. I want 1 button to add 1 to the amount and the other to decrease the amount by 1. But I don't know how to get the ID from the button I press. I have gotten this far.

I hope someone can let me know. How I can get the indexvalue depending on which button is pressed.

public class MainActivity extends AppCompatActivity {

    private ArrayList<Integer> ButtonUp;
    private ArrayList<Integer> Amount;
    private ArrayList<Integer> ButtonDown;
    int ArrayIndex = 0;
    int Value = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButtonUp();
        Amount();
        ButtonDown();
    }

    // ArrayList ButtonUp
    public void ButtonUp(){
        ButtonUp.add(R.id.Bup1);
        ButtonUp.add(R.id.Bup2);
    }

    // ArrayList Amount
    public void Amount(){
        Amount.add(R.id.Aantal1);
        Amount.add(R.id.Aantal2);
    }

    // ArrayList ButtonDown
    public void ButtonDown() {
        ButtonDown.add(R.id.Bdown1);
        ButtonDown.add(R.id.Bdown2);
    }

    // Get position ArrayList on press
    // Publish new Value
    public void buttonPress(View v) {
        switch (v.getId()) {
            case R.id.Bup1:
                SetAmountUp(id);
                displayQuantity(Value);
                break;
            case R.id.Bup2:
                SetAmountUp(R.id.Bup2);
                displayQuantity(Value);
                break;
            case R.id.Bdown1:
                SetAmountDown(R.id.Bdown1);
                displayQuantity(Value);
                break;
            case R.id.Bdown2:
                SetAmountDown(R.id.Bdown1);
                displayQuantity(Value);
                break;
        }
    }

    public void SetAmountUp(int indexNumberUp){
        Value = Amount.get(ButtonUp.indexOf(indexNumberUp));
        Value++;
        Amount.set(ArrayIndex,Value);
    }

    public void SetAmountDown(int indexNumberDown){
        Value = Amount.get(ButtonDown.indexOf(indexNumberDown));
        Value--;
        Amount.set(ArrayIndex,Value);
    }

    // Publish number
    private void displayQuantity(int NewAmount) {
        int ID = Amount.get(ArrayIndex);
        TextView quantityTextView = (TextView) findViewById(ID);
        quantityTextView.setText(NewAmount);
    }
}

Upvotes: 1

Views: 65

Answers (2)

akyirem samuel
akyirem samuel

Reputation: 78

It will be much easier for you if you used a recyclerView for that

Upvotes: 0

Lucas Queiroz Ribeiro
Lucas Queiroz Ribeiro

Reputation: 735

In the layout, declare an unique id to each button, then add android:onClick="increment" to each button.

In your class, create the method

public void increment(View view) {
  switch (view.getId()){
   case R.id.yourbuttonid1:
     // do what you whant
     break;
   case R.id.yourbuttonid2:
    // do with second editext
    break
    // ....
   }


}

Same approach to decrease method.

Upvotes: 1

Related Questions