Reputation: 5
The code below is a method that creates an alertdialog called from a menu bar, which has a spinner populated from a database. This dialog has a button which calls another method to open another alertdialog to add a new vendor to the spinner database.
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. 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.
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();
}
Upvotes: 0
Views: 57
Reputation: 3243
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
,
For that you what you do is, declare your spinnerArray
and dataAdapter
at class level,
then when you add a vendor, i.e inside vendorDialogBuilder
onClick()
function for positiveButton you can refresh the values like this,
final String vendorName = vendorNameET.getText().toString();
dbManager.insertVendor(vendorName);
spinnerArray = dbManager.getAllVendors();
dataAdapter.notifyDataSetChanged();
this will refresh the spinner values
Upvotes: 2