I have successfully created an App which uses CheckBoxes
, NumberPickers
and TextViews
(for each number picker). I also have a Toast
which now displays the number of each item checked, but the amount is static at the moment - i.e it only shows the single item price.
I want to use the NewVal from each number picker iteration to multiply the price. How do I take the int newVal
number and use it in the Listener which contains the Toast
?
Here's the code:
ps if you look at the first line of result.append, you'll see what I'm trying to do with newVal for EACH related number picker..
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.NumberPicker;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.NumberPicker.OnValueChangeListener;
public class MainActivity extends Activity {
int newVal;
private TextView tv;
private TextView[] tvs;
Button btnDisplay;
private int i;
private int j;
private int[] amount;
private CheckBox chk1, chk2, chk3, chk7, chk8, chk9, chk10, chk11, chk12, chk13, chk14, chk15,
chk16, chk17, chk18, chk19, chk20, chk21, chk22, chk23, chk24, chk25, chk26, chk27, chk28, chk29, chk30, chk31, chk32, chk33, chk34, chk35, chk36, chk37, chk38, chk39, chk40, chk41, chk42, chk43, chk44, chk45, chk46, chk47, chk48, chk49, chk50, chk51, chk52, chk53;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUI();
addListenerOnChkIos();
}
public void setupUI()
{
int[] textViewIDs = new int[] {R.id.tvId1, R.id.tvId2, R.id.tvId3, R.id.tvId7,
R.id.tvId8, R.id.tvId9, R.id.tvId10, R.id.tvId11, R.id.tvId12, R.id.tvId13, R.id.tvId14, R.id.tvId15, R.id.tvId16, R.id.tvId17,
R.id.tvId18, R.id.tvId19, R.id.tvId20, R.id.tvId21, R.id.tvId22, R.id.tvId23, R.id.tvId24, R.id.tvId25, R.id.tvId26, R.id.tvId27, R.id.tvId28, R.id.tvId29, R.id.tvId30, R.id.tvId31,
R.id.tvId32, R.id.tvId33, R.id.tvId34, R.id.tvId35, R.id.tvId36, R.id.tvId37, R.id.tvId38, R.id.tvId39, R.id.tvId40, R.id.tvId41, R.id.tvId42, R.id.tvId43, R.id.tvId44, R.id.tvId45, R.id.tvId46, R.id.tvId47, R.id.tvId48, R.id.tvId49, R.id.tvId50, R.id.tvId51, R.id.tvId52, R.id.tvId53};
tvs = new TextView[53];
for(int i=0; i < textViewIDs.length; i
++) {
tv = (TextView ) findViewById(textViewIDs[i]);
tvs[i] = tv;
}
//tv = (TextView) findViewById(R.id.tvId1);
int[] numpickIDs = new int[] {R.id.npId1, R.id.npId2, R.id.npId3, R.id.npId7,
R.id.npId8, R.id.npId9, R.id.npId10, R.id.npId11, R.id.npId12, R.id.npId13, R.id.npId14, R.id.npId15, R.id.npId16, R.id.npId17,
R.id.npId18, R.id.npId19, R.id.npId20, R.id.npId21, R.id.npId22, R.id.npId23, R.id.npId24, R.id.npId25, R.id.npId26, R.id.npId27, R.id.npId28, R.id.npId29, R.id.npId30, R.id.npId31,
R.id.npId32, R.id.npId33, R.id.npId34, R.id.npId35, R.id.npId36, R.id.npId37, R.id.npId38, R.id.npId39, R.id.npId40, R.id.npId41, R.id.npId42, R.id.npId43, R.id.npId44, R.id.npId45, R.id.npId46, R.id.npId47, R.id.npId48, R.id.npId49, R.id.npId50, R.id.npId51, R.id.npId52, R.id.npId53};
for(int j=0; j < numpickIDs.length; j++) {
NumberPicker np = (NumberPicker ) findViewById(numpickIDs[j]);
//NumberPicker np = (NumberPicker) findViewById(R.id.npId1);
final TextView curTextView = tvs[j];
np.setOnValueChangedListener(new OnValueChangeListener()
{
public void onValueChange(NumberPicker picker, int oldVal,
int newVal)
{
curTextView.setText(String.valueOf(newVal));
//tvs[i].setText(String.valueOf(newVal));
}
});
np.setMaxValue(12);
np.setMinValue(0);
}
}
public void addListenerOnChkIos() {
chk1 = (CheckBox) findViewById(R.id.chk1);
chk2 = (CheckBox) findViewById(R.id.chk2);
chk3 = (CheckBox) findViewById(R.id.chk3);
chk7 = (CheckBox) findViewById(R.id.chk7);
chk8 = (CheckBox) findViewById(R.id.chk8);
chk9 = (CheckBox) findViewById(R.id.chk9);
chk10 = (CheckBox) findViewById(R.id.chk10);
chk11 = (CheckBox) findViewById(R.id.chk11);
chk12 = (CheckBox) findViewById(R.id.chk12);
chk13 = (CheckBox) findViewById(R.id.chk13);
chk14 = (CheckBox) findViewById(R.id.chk14);
chk15 = (CheckBox) findViewById(R.id.chk15);
chk16 = (CheckBox) findViewById(R.id.chk16);
chk17 = (CheckBox) findViewById(R.id.chk17);
chk18 = (CheckBox) findViewById(R.id.chk18);
chk19 = (CheckBox) findViewById(R.id.chk19);
chk20 = (CheckBox) findViewById(R.id.chk20);
chk21 = (CheckBox) findViewById(R.id.chk21);
chk22 = (CheckBox) findViewById(R.id.chk22);
chk23 = (CheckBox) findViewById(R.id.chk23);
chk24 = (CheckBox) findViewById(R.id.chk24);
chk25 = (CheckBox) findViewById(R.id.chk25);
chk26 = (CheckBox) findViewById(R.id.chk26);
chk27 = (CheckBox) findViewById(R.id.chk27);
chk28 = (CheckBox) findViewById(R.id.chk28);
chk29 = (CheckBox) findViewById(R.id.chk29);
chk30 = (CheckBox) findViewById(R.id.chk30);
chk31 = (CheckBox) findViewById(R.id.chk31);
chk32 = (CheckBox) findViewById(R.id.chk32);
chk33 = (CheckBox) findViewById(R.id.chk33);
chk34 = (CheckBox) findViewById(R.id.chk34);
chk35 = (CheckBox) findViewById(R.id.chk35);
chk36 = (CheckBox) findViewById(R.id.chk36);
chk37 = (CheckBox) findViewById(R.id.chk37);
chk38 = (CheckBox) findViewById(R.id.chk38);
chk39 = (CheckBox) findViewById(R.id.chk39);
chk40 = (CheckBox) findViewById(R.id.chk40);
chk41 = (CheckBox) findViewById(R.id.chk41);
chk42 = (CheckBox) findViewById(R.id.chk42);
chk43 = (CheckBox) findViewById(R.id.chk43);
chk44 = (CheckBox) findViewById(R.id.chk44);
chk45 = (CheckBox) findViewById(R.id.chk45);
chk46 = (CheckBox) findViewById(R.id.chk46);
chk47 = (CheckBox) findViewById(R.id.chk47);
chk48 = (CheckBox) findViewById(R.id.chk48);
chk49 = (CheckBox) findViewById(R.id.chk49);
chk50 = (CheckBox) findViewById(R.id.chk50);
chk51 = (CheckBox) findViewById(R.id.chk51);
chk52 = (CheckBox) findViewById(R.id.chk52);
chk53 = (CheckBox) findViewById(R.id.chk53);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
//chk1.setOnClickListener(new OnClickListener() {
//Run when button is clicked
@Override
public void onClick(View v) {
//StringBuffer result = new StringBuffer();
//result.append("Haddock-Large : ").append(chk1.isChecked());
//result.append("\nHaddock-Medium : ").append(chk2.isChecked());
//result.append("\nHaddock-Small :").append(chk3.isChecked());
//result.append("\nKids Corner : ").append(chk4.isChecked());
//Toast.makeText(MainActivity.this, tv.getText().toString() + " " + result.toString(),
//Toast.LENGTH_LONG).show();
// if (((CheckBox) v).isChecked()) {
//Toast.makeText(MainActivity.this, tv.getText().toString() + " " + "Haddock Large",
//Toast.LENGTH_LONG).show();
//}
//}
//});
int totalamount=0;
//final TextView cur1TextView = tvs[j];
StringBuilder result=new StringBuilder();
result.append("Selected Items:\n");
if(chk1.isChecked()){
result.append(tvs[0].getText().toString() + "x" + "Haddock(Large) (newVal ^ 100)Rs");
totalamount+=100;
}
if(chk2.isChecked()){
result.append(tvs[1].getText().toString() + "x" + "\nHaddock(Med) 50Rs");
totalamount+=50;
}
if(chk3.isChecked()){
result.append(tvs[2].getText().toString() + "x" + "\nHaddock(Small) 120Rs");
totalamount+=120;
}
if(chk7.isChecked()){
result.append(tvs[3].getText().toString() + "x" + "\nChips 120Rs");
totalamount+=120;
}
if(chk8.isChecked()){
result.append(tvs[4].getText().toString() + "x" + "\nCone of Chips 120Rs");
totalamount+=120;
}
if(chk9.isChecked()){
result.append(tvs[5].getText().toString() + "x" + "\nChip Butty 120Rs");
totalamount+=120;
}
if(chk10.isChecked()){
result.append(tvs[6].getText().toString() + "x" + "\nFries 120Rs");
totalamount+=120;
}
if(chk11.isChecked()){
result.append(tvs[7].getText().toString() + "x" + "\nPattie 120Rs");
totalamount+=120;
}
if(chk12.isChecked()){
result.append(tvs[8].getText().toString() + "x" + "\nPattie Buttie 120Rs");
totalamount+=120;
}
if(chk13.isChecked()){
result.append(tvs[9].getText().toString() + "x" + "\nFish Cake 120Rs");
totalamount+=120;
}
if(chk14.isChecked()){
result.append(tvs[10].getText().toString() + "x" + "\nBreadcakes 120Rs");
totalamount+=120;
}
if(chk15.isChecked()){
result.append(tvs[11].getText().toString() + "x" + "\nHam Fritter 120Rs");
totalamount+=120;
}
if(chk16.isChecked()){
result.append(tvs[12].getText().toString() + "x" + "\nCorned Beef Fritter 120Rs");
totalamount+=120;
}
if(chk17.isChecked()){
result.append(tvs[13].getText().toString() + "x" + "\nFish Fritter 120Rs");
totalamount+=120;
}
if(chk18.isChecked()){
result.append(tvs[14].getText().toString() + "x" + "\nBeef Burger 120Rs");
totalamount+=120;
}
if(chk19.isChecked()){
result.append(tvs[15].getText().toString() + "x" + "\nSmall Sausage 120Rs");
totalamount+=120;
}
if(chk20.isChecked()){
result.append(tvs[16].getText().toString() + "x" + "\nJumbo Sausage 120Rs");
totalamount+=120;
}
if(chk21.isChecked()){
result.append(tvs[17].getText().toString() + "x" + "\nPickled Eggs 120Rs");
totalamount+=120;
}
if(chk22.isChecked()){
result.append(tvs[18].getText().toString() + "x" + "\nPickled Onions 120Rs");
totalamount+=120;
}
if(chk23.isChecked()){
result.append(tvs[19].getText().toString() + "x" + "\n8 Onion Rings 120Rs");
totalamount+=120;
}
if(chk24.isChecked()){
result.append(tvs[20].getText().toString() + "x" + "\nScampi(8) & chips/fries 120Rs");
totalamount+=120;
}
if(chk25.isChecked()){
result.append(tvs[21].getText().toString() + "x" + "\nPattie Bitesx8 120Rs");
totalamount+=120;
}
if(chk26.isChecked()){
result.append(tvs[22].getText().toString() + "x" + "\nHotdog"
+ " 120Rs");
totalamount+=120;
}
if(chk27.isChecked()){
result.append(tvs[23].getText().toString() + "x" + "\nHotdog w/chip/fries and a can 120Rs");
totalamount+=120;
}
if(chk28.isChecked()){
result.append(tvs[24].getText().toString() + "x" + "\nHome Made Fish Cakes 120Rs");
totalamount+=120;
}
if(chk29.isChecked()){
result.append(tvs[25].getText().toString() + "x" + "\nCod Roe 120Rs");
totalamount+=120;
}
if(chk30.isChecked()){
result.append(tvs[26].getText().toString() + "x" + "\n4 Fish Strips 120Rs");
totalamount+=120;
}
if(chk31.isChecked()){
result.append(tvs[27].getText().toString() + "x" + "\nChicken & Mushroom 120Rs");
totalamount+=120;
}
if(chk32.isChecked()){
result.append(tvs[28].getText().toString() + "x" + "\nSteak & Kidney 120Rs");
totalamount+=120;
}
if(chk33.isChecked()){
result.append(tvs[29].getText().toString() + "x" + "\nBeef & Onion 120Rs");
totalamount+=120;
}
if(chk34.isChecked()){
result.append(tvs[30].getText().toString() + "x" + "\n1/4lb Beef Burger 120Rs");
totalamount+=120;
}
if(chk35.isChecked()){
result.append(tvs[31].getText().toString() + "x" + "\n1/2lb Beef Burger 120Rs");
totalamount+=120;
}
if(chk36.isChecked()){
result.append(tvs[32].getText().toString() + "x" + "\n1/4ib Cheese Burger 120Rs");
totalamount+=120;
}
if(chk37.isChecked()){
result.append(tvs[33].getText().toString() + "x" + "\n1/2lb Cheese Burger 120Rs");
totalamount+=120;
}
if(chk38.isChecked()){
result.append(tvs[34].getText().toString() + "x" + "\nChicken Burger 120Rs");
totalamount+=120;
}
if(chk39.isChecked()){
result.append(tvs[35].getText().toString() + "x" + "\nDouble Chicken Burger 120Rs");
totalamount+=120;
}
if(chk40.isChecked()){
result.append(tvs[36].getText().toString() + "x" + "\nPot of Peas 120Rs");
totalamount+=120;
}
if(chk41.isChecked()){
result.append(tvs[37].getText().toString() + "x" + "\nChips spice tub 120Rs");
totalamount+=120;
}
if(chk42.isChecked()){
result.append(tvs[38].getText().toString() + "x" + "\nSalt 250g 120Rs");
totalamount+=120;
}
if(chk43.isChecked()){
result.append(tvs[39].getText().toString() + "x" + "\nVinegar 284ml 120Rs");
totalamount+=120;
}
if(chk44.isChecked()){
result.append(tvs[40].getText().toString() + "x" + "\nTomato Sauce 120Rs");
totalamount+=120;
}
if(chk45.isChecked()){
result.append(tvs[41].getText().toString() + "x" + "\nCans 120Rs");
totalamount+=120;
}
if(chk46.isChecked()){
result.append(tvs[42].getText().toString() + "x" + "\nKids Cup Drinks 120Rs");
totalamount+=120;
}
if(chk47.isChecked()){
result.append(tvs[43].getText().toString() + "x" + "\nBottled Water 120Rs");
totalamount+=120;
}
if(chk48.isChecked()){
result.append(tvs[44].getText().toString() + "x" + "\n2ltr Bottles star 120Rs");
totalamount+=120;
}if(chk49.isChecked()){
result.append(tvs[45].getText().toString() + "x" + "\nKIDS CORNER 120Rs");
totalamount+=120;
}
if(chk50.isChecked()){
result.append(tvs[46].getText().toString() + "x" + "\nFISH SPECIAL 120Rs");
totalamount+=120;
}
if(chk51.isChecked()){
result.append(tvs[47].getText().toString() + "x" + "\nALL DAY SPECIALS 120Rs");
totalamount+=120;
}
if(chk52.isChecked()){
result.append(tvs[48].getText().toString() + "x" + "\nFAMILY MEAL DEAL 120Rs");
totalamount+=120;
}
result.append("\nTotal: "+totalamount+"Rs");
//Displaying the message on the toast
Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show();
}
});
}
}
Aucun commentaire:
Enregistrer un commentaire