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