0

imagine a list of products (food dishes for a restaurant) you can order, to the right is a minus button, text view showing the quantity and a plus button. I have used a for loop because i dont know how many products. I have used a series of arrays....

public class menuActivity
    extends Activity
    implements OnClickListener
{
    String[] dishes = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
    int dishQuantity =10;
    LinearLayout linear;
    LinearLayout L1, L2;
    TableLayout table;
    Button[] plus = new Button[dishQuantity];
    Button[] minus = new Button[dishQuantity];
    TextView[] text = new TextView[dishQuantity];
    TableRow[] row = new TableRow[dishQuantity];
    TextView[] quantityArray = new TextView[dishQuantity];
    int[] quantityValue = new int[dishQuantity];

    // declares import of widgets
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        linear = new LinearLayout(this);
        table = new TableLayout(this);
        linear.setOrientation(LinearLayout.VERTICAL);
        // declares Layouts and sets Properties
        for (int i =0; i<dishQuantity; i++){
            quantityValue[i]=0;
        }

        // sets quantityValue for each dish to 0
        for (int i=0; i<dishQuantity; i++) {

             row[i] = new TableRow(this);
             text[i] = new TextView(this);
             plus[i] = new Button(this); 
             minus[i] = new Button(this);
             quantityArray[i] = new TextView(this);

             // declares import arrays
             plus[i].setText("+");
             minus[i].setText("-");          
             text[i].setText(dishes[i]);
             quantityArray[i].setText(String.valueOf(quantityValue[i]));

             // sets values
             plus[i].setOnClickListener(this);
             minus[i].setOnClickListener(this);

             row[i].addView(text[i]);
             row[i].addView(minus[i]);
             row[i].addView(quantityArray[i]);
             row[i].addView(plus[i]);

             // adds all widgets to corresponding row
             table.addView(row[i]);
             // adds current row to TableLayout
        } 

        linear.addView(table);

        // adds current table to parent Linear Layout - linear
        setContentView(linear);
        // sets parent Linear Layout - linear to content

    }

    public void onClick(View arg0) {
    // TODO Auto-generated method stub

}



}

Obviously if they click the plus button for product 2 i need the the quantityValue for product 2 to increment, but i cannot work out how to set this up in the onClick part and separate out the buttons

Also how do i change the gravity of the buttons so they are right aligned

plus[0].setGravity(RIGHT);

Any help is greatly appreciated

3 Answers 3

2

What you could do is give each button an id you get from your array, and assign it with setId().

Then you could get the id from the view in your onclick, so you know what button was pressed:

public void onClick(View arg0) {
    //use a switch with cases for each of the known ID's, and add your code there.
}

as said in comments, quickly, so sorry for any mistakes On second thought, add your id in the loop where you make the buttons. Then make a function that finds the correct item connected to this id. Then you can do something like:

public void onClick(View arg0) {
    int myId = arg0.getId();
    int myDish = findDishFromId(myId);
    addOne(myDish);
}

So you'd make findDishFromId, where you find the correct dish. Probably something connected to that i in the loop. Than, when you've found the dish, you can addOne or substractone, or whatever.

Sorry I can't be more specific, gotta run :D

Sign up to request clarification or add additional context in comments.

2 Comments

Can you type the code for the switch statement, specifically the argument in the brackets, it confuses me because i am used to defining buttons from an XML layout by using findViewById
I was about to log-off, but quickly, sorry for any mistakes :)
1

You can also create private class which will handle each row's elements, like provided example below. It will take care about it's elements and will be simple to change things in a single place. In this class, you will know that click on + or - button should change value in it's TextView.

private class OneRowHandler implements OnClickListener
{
Button plus;
Button minus;
TextView quantity;

public OneRowHandler(Button pl, Button mi, TextView q) {
    plus = pl;
    minus = mi;
            quantity = q;
    plus.setOnClickListener(this);
    minus.setOnClickListener(this);
}

public void onClick(View arg0) {
    if (arg0 == plus) {
        // do ++
    } else if (arg0 == minus) {
        // do --
    }
}
}

Comments

0

I think that you need to set tag for each button before you add them like this:

            View bv = (View) myButton;
            bv.setTag(value);

For value you can use an integer like i++. Then on click

buttonCode = (Integer) v.getTag();

will tell you exactly which button you have pressed. You can store anything in view's tag such as references and objects.

for each button you can set padding before you use addView and other properties

myButton.setPadding(10, 5, 5, 10);

3 Comments

by the way I don't see any point in referencing buttons and rows into arrays unless you want to change their properites later in the program.
I am new to android, can you suggest and explain a better way to list a list of products (which may change in number) with a plus and minus button like you might find on a websites shopping cart?
The proper way is to use ListView with a custom layout which contains image, name, quantity and plus and minus button. You need to learn fist how to make a more complex ListView type lists, and use tables only for fixed and small number of rows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.