简体   繁体   English

从数据库更新微调器值

[英]Update spinner values from database

The code below is a method that creates an alertdialog called from a menu bar, which has a spinner populated from a database. 下面的代码是一种创建从菜单栏调用的alertdialog的方法,该菜单栏中有一个从数据库填充的微调框。 This dialog has a button which calls another method to open another alertdialog to add a new vendor to the spinner database. 该对话框具有一个按钮,该按钮调用另一种方法来打开另一个Alertdialog,以向旋转器数据库中添加新的供应商。

My question is, is there a way to make it so after the showAddVendorDialog adds a new value to the database, it updates the spinner with the recently added value. 我的问题是,有没有办法做到这一点,所以在showAddVendorDialog向数据库添加新值之后,它将使用最近添加的值更新微调器。 Right now if I close the dialog from showAddDialog, it will show the new value when I open it again, but I would like it to show the new value without having to close it. 现在,如果我从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();
}

Yes you can do that, for that when you add a vendor from the addVendorDialog you will need to refresh the showVendroDialog DataSet , and for that you will have to refresh the Spinner Adapter , 是的,您可以这样做,为此,当您从addVendorDialog添加供应商时,您将需要刷新showVendroDialog DataSet ,并且为此,您必须刷新Spinner Adapter

For that you what you do is, declare your spinnerArray and dataAdapter at class level, 为此,您需要在类级别声明spinnerArraydataAdapter

then when you add a vendor, ie inside vendorDialogBuilder onClick() function for positiveButton you can refresh the values like this, 那么当你添加一个供应商,即内部vendorDialogBuilder onClick()函数positiveButton你可以刷新这样的价值观,

final String vendorName = vendorNameET.getText().toString();
dbManager.insertVendor(vendorName);

spinnerArray = dbManager.getAllVendors();
dataAdapter.notifyDataSetChanged();

this will refresh the spinner values 这将刷新微调器的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM