Android AlertDialog等待调用活动的结果

时间:2013-10-22 03:39:41

标签: android android-alertdialog

我正在尝试在我的应用中使用AlertDialog来选择项目的数量。问题是,调用AlertDialog的活动在将项目添加到SQLite数据库并更改意图之前不会等待它更新项目。

此时,QuantitySelectorAlertDialog)出现,然后立即消失并更改MealActivity类(只是从数据库中读取的ListView )通过对数量为0的数据库更新进行意图更改。

我需要Activity在更新数据库之前等待AlertDialog关闭。

实现这个的正确方法是什么?

以下是一些代码:

QuantitySelector(运行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();
    }

}

favouritesAdapter(调用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);

            }
        });

感谢所有帮助:)

3 个答案:

答案 0 :(得分:0)

使用异步任务并更新doInBackGround和onPostExecute方法Show Dialog中的数据。

答案 1 :(得分:0)

你想要解决这个问题的方法是当人按下正按钮时实际开始下一个意图。简而言之,您需要在附加到AlertDialog的正面按钮的OnClickListener中启动下一个Activity。

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();

                    //The only catch now is passing through your _context
                    Intent intent = new Intent(_context,MealActivity.class);
                    _context.startActivity(intent);
                }
            })
            .setNegativeButton("Cancel", null).create();
        dialog.show();
}

答案 2 :(得分:0)

实际上你的问题是你在销毁警报对话框之前调用了MealACtivity的开始活动,所以可以按如下方式更新你的代码:

更新通过以下代码调用alertdialogue的方法:

    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);*/

        }
    });

并使用以下内容更新您的数量选择器类:

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);

                        Intent intent = new Intent(_context,MealActivity.class);
                        _context.startActivity(intent);

                        dialog.dismiss();
                    }
                })
                .setNegativeButton("Cancel", null).create();
            dialog.show();
    }