Android AlertDialog wait for result in calling activity

Posted by insanesam on Stack Overflow See other posts from Stack Overflow or by insanesam
Published on 2013-10-22T03:39:41Z Indexed on 2013/10/22 3:53 UTC
Read the original article Hit count: 393

I am trying to use an AlertDialog in my app to select the quantity of an item. The problem is that the activity that calls the AlertDialog doesn't wait for it to update the item before it adds it to the SQLite Database and change intents.

At the moment, the QuantitySelector (AlertDialog) appears, then disappears straight away and changes the MealActivity class (which is just a ListView that reads from the database) through the intent change with an update to the database with quantity 0.

I need the Activity to wait for the AlertDialog to close before it updates the database.

What would be the correct way of implementing this?

Here is some code for you:

QuantitySelector (which runs the alertdialog):

public class QuantitySelector{

    protected static final int RESULT_OK = 0;
    private Context _context;
    private DatabaseHandler db;
    private HashMap<String, Double> measures;
    private Item item;
    private View v;
    private EditText quan;
    private NumberPicker pick;
    private int value;
    private Quantity quantity;

    /**
     * Function calls the quantity selector AlertDialog
     * @param _c: The application context
     * @param item: The item to be added to consumption
     * @return The quantity that is consumed
     */
    public void select(Context _c, Item item, Quantity quantity){
        this._context = _c;
        this.item = item;
        this.quantity = quantity;
        db = new DatabaseHandler(_context);
        //Get the measures to display
        createData();
        //Set up the custom view
        LayoutInflater inflater = LayoutInflater.from(_context);
        v = inflater.inflate(R.layout.quantity_selector, null);
        //Set up the input fields
        quan = (EditText) v.findViewById(R.id.quantityNumber);
        pick = (NumberPicker) v.findViewById(R.id.numberPicker1);
        //Set up the custom measures into pick
        pick.setMaxValue(measures.size()-1);
        pick.setDisplayedValues(measures.keySet().toArray(new String[0]));
        //Start the alert dialog
        runDialog();
    }

    public void createData(){
        measures = new HashMap<String, Double>();       
        //Get the measurements from the database
        if(item!=null){
        measures.putAll(db.getMeasures(item));
        }
        //Add grams as the default measurement
        if(!measures.keySet().contains("grams")){
            //Add grams as a standard measure
            measures.put("grams", 1.0);
        }
    }

    public void runDialog(){
        AlertDialog dialog = new AlertDialog.Builder(_context).setTitle("Select Quantity")
                .setView(v)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        //Change the consumption to the new quantity
                        if(!quan.getText().toString().matches("")){
                            value = Integer.parseInt(quan.getText().toString());
                            //Check if conversion from other units is needed
                            String s[] = pick.getDisplayedValues();
                            String a = s[pick.getValue()];
                            //Convert the chosen measure back to grams
                            if(!a.equals("grams")){
                                for(String m : measures.keySet()){
                                    if(m==a){
                                        value = (int) (value * measures.get(m));
                                    }
                                }
                            }
                        }
                        quantity.setQuantity(value);
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("Cancel", null).create();
            dialog.show();
    }

}

The method from favouritesAdapter (which calls the alertdialog):

add.setOnClickListener(new OnClickListener(){

            public void onClick(View arg0) {
                QuantitySelector q = new QuantitySelector();
                Quantity quan = new Quantity();
                q.select(_context, db.getItem(p.getID()), quan);
                db.addConsumption(p.getID(), p.getFavouriteShortName(), quan.getQuantity(), "FAVOURITE");
                Intent intent = new Intent(_context,MealActivity.class);
                _context.startActivity(intent);

            }
        });

All help is appreciated :)

© Stack Overflow or respective owner

Related posts about android

Related posts about android-alertdialog