从数据库更新微调器值

时间:2016-05-31 03:49:24

标签: java android

下面的代码是一个创建从菜单栏调用的alertdialog的方法,该菜单栏有一个从数据库填充的微调器。此对话框有一个按钮,该按钮调用另一种方法打开另一个alertdialog,以将新供应商添加到微调器数据库。

我的问题是,有没有办法在showAddVendorDialog向数据库添加新值之后这样做,它会使用最近添加的值更新微调器。现在如果我从showAddDialog关闭对话框,它会在我再次打开它时显示新值,但我希望它显示新值而不必关闭它。

protected void showAddDialog() {

    LayoutInflater layoutInflater = LayoutInflater.from(FlavorList.this);
    View promptView = layoutInflater.inflate(R.layout.add_flavor_alert, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(FlavorList.this);
    alertDialogBuilder.setView(promptView);

    alertDialogBuilder.setTitle("Add Flavor");

    final List<String> spinnerArray = dbManager.getAllVendors();

    final ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this,
            android.R.layout.simple_spinner_item, spinnerArray);

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    final EditText nameET = (EditText) promptView.findViewById(R.id.flavorname_add);
    vendorSpinner = (Spinner) promptView.findViewById(R.id.vendor_spinner);
    //final EditText brandET = (EditText) promptView.findViewById(R.id.flavorbrand_add);
    final EditText weightET = (EditText) promptView.findViewById(R.id.flavorweight_add);
    final Button addVendorButton = (Button) promptView.findViewById(R.id.btn_addvendor);
    addVendorButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){

            showAddVendorDialog();
            dataAdapter.notifyDataSetChanged();

        }

    });//end of addVendorButton listener

    vendorSpinner.setAdapter(dataAdapter);


    alertDialogBuilder.setCancelable(false)
            .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    final String name = nameET.getText().toString();
                    final String brand = vendorSpinner.getSelectedItem().toString();
                    final double weight = Double.valueOf(weightET.getText().toString());

                    dbManager.insertFlavor(name, brand, weight);

                    Intent main = new Intent(getApplicationContext(), FlavorList.class)
                            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    startActivity(main);
                }
            })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });


    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
}//end showModifyDialog

protected void showAddVendorDialog(){
    LayoutInflater vendorInflater = FlavorList.this.getLayoutInflater();
    View vendorView = vendorInflater.inflate(R.layout.add_vendor_alert, null);
    AlertDialog.Builder vendorDialogBuilder = new AlertDialog.Builder(FlavorList.this);
    vendorDialogBuilder.setView(vendorView);

    vendorDialogBuilder.setTitle("Add New Vendor");

    final EditText vendorNameET = (EditText) vendorView.findViewById(R.id.new_vendor);
    vendorDialogBuilder.setCancelable(false)
            .setPositiveButton("Add", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int id){
                    final String vendorName = vendorNameET.getText().toString();

                    dbManager.insertVendor(vendorName);

                }
            })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

    AlertDialog vendorAlert = vendorDialogBuilder.create();
    vendorAlert.show();
}

1 个答案:

答案 0 :(得分:2)

是的,您可以这样做,因为当您从addVendorDialog添加供应商时,您需要刷新showVendroDialog DataSet,为此,您必须刷新{ {1}},

为此你所做的就是在班级宣布你的Spinner AdapterspinnerArray

然后当您添加供应商时,即在 positiveButton dataAdapter vendorDialogBuilder函数内,您可以刷新这样的值,

onClick()

这将刷新微调器值