hi im getting a problem on my checkbox when check is change and if the checkbox is checked im displaying a alert dialog which take a input value and set the value to the text view and also im adding that value to the List here is my code
@Override
public void onCheckedChanged(CompoundButton checkBox, boolean isChecked)
{
TextView tvItem=(TextView)selectedView.findViewById(R.id.foodName);
final TextView tvQuantity=(TextView)selectedView.findViewById(R.id.quantity);
orderList=new ArrayList<Order>();
Order order=new Order();
if(isChecked)
{
final AlertDialog.Builder quantityAlert= new AlertDialog.Builder(TakeOrder.this);
quantityAlert.setTitle("Quantity");
quantityAlert.setMessage("Please enter quantity");
final EditText input = new EditText(TakeOrder.this);
input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
quantityAlert.setView(input);
quantityAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
value = input.getText();
tvQuantity.setText(value);
}
});
quantityAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
}
});
quantityAlert.show();
order.setItem(tvItem.getText().toString());
order.setQuantity(Integer.valueOf(tvQuantity.getText().toString()));
orderList.add(selectedPosition, order);
Log.v("Item:",orderList.get(selectedPosition).getItem());
Log.v("Quantity:",String.valueOf(orderList.get(selectedPosition).getQuantity()));
}
else
{
tvQuantity.setText("0");
orderList.remove(selectedPosition);
}
}
The problem is that im showing the alert dialog first then taking the value of quantity from user but before i enter the value in alert box the Log.v executes and shows 0 that means in my List quantity is being added to 0…
but i want that the quantity value coming from alert dialog should be added to the List….
Can any one please help me…
This makes sense becuase the code doesn’t stop and wait for your Alert Dialog to finish. It builds it, displays it, then moves on, which is where you are creating the value and adding it to the list.
If you want it to only add when you press “Ok”, you need to put the Order modification and insertion code inside your “Ok” (positive button) handler.
I also put the Order intialization statement inside the onClick method because it isn’t used anywhere else. Save yourself one line of code if they select “Cancel” 🙂