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 :)
Use Async task and update data in doInBackGround and in onPostExecute method Show Dialog.
The way you want to go about this is to actually start the next intent when the person presses the positive button. In short, you need to be starting your next Activity in the OnClickListener that is attached to your positive button of your AlertDialog.
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();
}
Actually your problem is you are calling the start activity for MealACtivity before destroying the alert dialogue so can update your code as follows:
Update your method which calls the alertdialogue by this code:
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);*/
}
});
and update your Quantity Selector class with the following :
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();
}
Related
I am using a Dialog to show a sequence of quizzes in Android. I would like to show the next quiz once the user answers True or False.
I am using a custom layout and I am controlling it with the following code:
public void showQuizDialog() {
View v;
TextView question;
// Textview showing Quiz 1 out of 3
TextView numQ;
NetworkImageView image;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
// The coordinator keeps track of the game
coordinator.setQuizStarted();
// Get the quiz using the coordinator
Quiz qX = coordinator.getQuiz();
final String answer = qX.getAnswer();
final String quizTracker = coordinator.getTracker();
// Create an instance of the dialog fragment
AlertDialog.Builder quiz = new AlertDialog.Builder(MyActivity.this);
LayoutInflater inflater = MyActivity.this.getLayoutInflater();
// Inflate and set the layout for the dialog
v = inflater.inflate(R.layout.quiz_layout,null);
// Set the question
question = (TextView) v.findViewById(R.id.content_quiz);
question.setText(qX.getQuestion());
// Set current number of question
numQ = (TextView) v.findViewById(R.id.number_quiz);
numQ.setText(quizTracker);
// Retrieves the image from url
image = (NetworkImageView) v.findViewById(R.id.thumbnail_quiz);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
String urlImage = getCompleteUrl(CODE_QUIZ_IMG);
urlImage += qX.getImg();
image.setImageUrl(urlImage, imageLoader);
quiz.setPositiveButton("True", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
if (answer.equals(quiz_true)) {
coordinator.notifyCorrectQuizNum();
Log.d(QUIZ_DIALOG,"\t\tCorrect answer! it was true");
}
coordinator.setQuizEnded(true);
coordinator.updateQuizLeft();
}
})
.setNegativeButton("False", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked to False btn
if (answer.equals(quiz_false)){
Log.d(QUIZ_DIALOG,"\t\tCorrect Answer!! It was false");
coordinator.notifyCorrectQuizNum();
}
coordinator.setQuizEnded(true);
coordinator.updateQuizLeft();
}
});
quiz.setView(v);
final AlertDialog dialog = quiz.create();
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
Let's say there are 100 quizzes to show and a method of the Coordinator class that returns the number of remaining quizzes.
So, after the call
coordinator.setQuizEnded(true);
coordinator.updateQuizLeft();
the coordinator.getQuizLeft() will return 99. And coordinator.getQuiz() will point to the next quiz.
How do I update the TextView question and numQ without closing the dialog?
Try something like this :
setupQuizDialog() which is called first and creates a dialog without any content.
loadNewQuestionIntoDialog() which is continuously called to refresh the content of the dialog with a new question.
public void setupQuizDialog() {
LayoutInflater inflater = MyActivity.this.getLayoutInflater();
// Inflate and set the layout for the dialog
View v = inflater.inflate(R.layout.quiz_layout,null);
AlertDialog.Builder quiz = new AlertDialog.Builder(MyActivity.this)
.setPositiveButton("True", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Nothing, will replace later
}
})
.setNegativeButton("False", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Nothing, will replace later
}
});
quiz.setView(v);
final AlertDialog dialog = quiz.create();
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
loadNewQuestionIntoDialog(dialog, 100);
}
public void loadNewQuestionIntoDialog(final AlertDialog dialog, final int questionNumber){
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
// The coordinator keeps track of the game
coordinator.setQuizStarted();
// Get the quiz using the coordinator
final String answer = qX.getAnswer();
final String quizTracker = coordinator.getTracker();
TextView question = (TextView) dialog.findViewById(R.id.content_quiz);
question.setText(qX.getQuestion());
TextView numQ = (TextView) dialog.findViewById(R.id.number_quiz);
numQ.setText(quizTracker);
// Retrieves the image from url
NetworkImageView image = (NetworkImageView) dialog.findViewById(R.id.thumbnail_quiz);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
String urlImage = getCompleteUrl(CODE_QUIZ_IMG);
urlImage += qX.getImg();
image.setImageUrl(urlImage, imageLoader);
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// User clicked OK button
if (answer.equals(quiz_true)) {
coordinator.notifyCorrectQuizNum();
Log.d(QUIZ_DIALOG,"\t\tCorrect answer! it was true");
}
coordinator.setQuizEnded(true);
if(questionNumber > 0)
loadNewQuestionIntoDialog(dialog, coordinator.updateQuizLeft());
}
});
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// User clicked to False btn
if (answer.equals(quiz_false)){
Log.d(QUIZ_DIALOG,"\t\tCorrect Answer!! It was false");
coordinator.notifyCorrectQuizNum();
}
coordinator.setQuizEnded(true);
if(questionNumber > 0)
loadNewQuestionIntoDialog(dialog, coordinator.updateQuizLeft());
}
});
}
Building my first app and can't find a solution by myself.
What my app does:
MainActivity prompts user to input player amount
presses Ok, input passed to next activity
now I want, that the user is prompted to input the player names, one by one with an AlertDialog. Those names, should be stored in an Array.
My code so far:
public class MainScreen extends AppCompatActivity {
private static final String TAG = MainScreen.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
try {
Intent intent = getIntent();
final int sumPlayers = getIntent().getIntExtra("sumPlayers", 0);
final List<String> playerNames = new ArrayList<>();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText input = new EditText(getBaseContext());
input.setTextColor(Color.BLACK);
//input.setSingleLine();
for (int c=0; c<sumPlayers; c++) {
builder.setTitle("Input Player Name");
builder.setView(input);
builder.setPositiveButton("ADD", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
playerNames.add(input.getText().toString());
}
});
builder.show();
}
ArrayAdapter<String> playerAdapter = new ArrayAdapter<String>(this, R.layout.player_list_item, R.id.editText, playerNames);
ListView listView = (ListView) findViewById(R.id.listView_main);
listView.setAdapter(playerAdapter);
} catch (Exception e) {
System.out.println("2te Act");
Log.e(TAG, "Error#: ", e);
}
}
}
I get this Exception # builder.show();
Java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
It's working without the for loop except one minor problem.
When I set the input field to setSingleLine(); the listView stays empty.
You are creating a single AlertDialog.Builder and repeatedly setting the title, view, and positive button with different values. You likely need to move this logic inside the for loop:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText input = new EditText(getBaseContext());
input.setTextColor(Color.BLACK);
//input.setSingleLine();
I'm using some number pickers from a xml-file in a alert dialog to get some coordinate inputs. The pickers are created and have some values (when you mark it and the keyboard opens you can see them), but won't show other values and the displayed value has the same color as the background.
When I press the OK-Button, the (more or less) displayed values are given correctly to the activity.
My Code:
public void showDialog()
{
final Context context=getApplicationContext();
final AlertDialog.Builder d = new AlertDialog.Builder(this);
final NumberPicker np1, np2, np3, np4, np5, np6, np7, np8;
final String abc[] = new String[] { "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z" };
final String zero_to_99[] = new String[100];
//init string array
for(int i=0; i<=99; i++)
{
zero_to_99[i] = Integer.toString(i);
if(zero_to_99[i].length() == 1)
zero_to_99[i] = "0"+Integer.toString(i);
}
LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.dialog_pick_coord,null);
String txt_title = context.getResources().getString(R.string.txt_head_search_coord);
d.setTitle(txt_title);
//Spalte
np1 = (NumberPicker) view.findViewById(R.id.p1);
np1.setMaxValue(60); // max value 60
np1.setMinValue(1); // min value 1
np1.setWrapSelectorWheel(false);
//Zeile
np2 = (NumberPicker) view.findViewById(R.id.p2);
np2.setMaxValue(25); // max value Z
np2.setMinValue(0); // min value A
np2.setDisplayedValues( abc );
np2.setWrapSelectorWheel(false);
//100km Quadrat 1
//more number pickers
//100km Quadrat 2
//more number pickers
//Easting xx*
//more number pickers
//Easting **x
//more number pickers
//Northing xx*
//more number pickers
//Northing **x
//more number pickers
np1.setValue(utmCoordElements[0]);
np2.setValue(utmCoordElements[1]);
np3.setValue(utmCoordElements[2]);
np4.setValue(utmCoordElements[3]);
np5.setValue(utmCoordElements[4]);
np6.setValue(utmCoordElements[5]);
np7.setValue(utmCoordElements[6]);
np8.setValue(utmCoordElements[7]);
d.setPositiveButton(context.getResources().getString(R.string.Accept), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Code for click on positive button
}
});
d.setNegativeButton(context.getResources().getString(R.string.Cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Code for click on negative button
}
});
d.setView(view);
d.show();
}
In my "main activity" I have a Button with a onClickListeners wich calls the showDialog() Method
In my main class i've a button to call a class which shows a dialog box with an edittext. My problem is this - Main activity is not getting edittext value at the first run, if i run it for a second time, i get the old edittext value.
It seems the main activity class executes the full block of code and returns a previous value which is stored in the class, i've tried many methods including shared preference.
MainActivity.java
public class MainActivity extends Activity {
EditText comment_et,input_et;
Spinner spinner;
Button addbutton,reportbut;
String input_string,date,time,comment,item;
TextView date_tv,time_tv;
String temp[];
Datas datatemp;
String savedinput;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
DatabaseHandler db = new DatabaseHandler(this);
#Override
protected void onCreate(Bundle savedInstanceState)
{
SharedPreferences prefs = getSharedPreferences("myprefs", 0);
savedinput= prefs.getString("KEY_SAVEDINPUT","");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spin1);
input_et = (EditText)findViewById(R.id.input_et);
addbutton = (Button)findViewById(R.id.addbutton);
reportbut = (Button)findViewById(R.id.report);
comment_et = (EditText)findViewById(R.id.comment_et);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
date_tv = (TextView)findViewById(R.id.date_tv);
time_tv = (TextView)findViewById(R.id.time_tv);
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
int mHour = c.get(Calendar.HOUR_OF_DAY);
int mMinute = c.get(Calendar.MINUTE);
date = ""+mDay+"/"+mMonth+1+"/"+mYear;
time = ""+mHour+":"+mMinute;
date_tv.setText(date);
time_tv.setText(time);
int max_id = db.getDatasCount();
for(int i = 1; i<max_id+1 ;i++)
{
datatemp = db.getItemOnly(i);
String s = datatemp._item.toString();
list.add(" "+ s);
}
addbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialogManager alert = new AlertDialogManager();
alert.showAlertDialog(MainActivity.this, "Enter Item",
"Please enter the spinner item",
true);
System.out.println("main : " +savedinput);
}
});
reportbut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), ListviewActivity.class);
startActivity(i);
}
});
spinner.setAdapter(adapter);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
item = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Button submit = (Button)findViewById(R.id.save);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
comment = comment_et.getText().toString();
System.out.println("comment:"+comment);
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addData(new Datas(item, comment, date, time));
Toast.makeText(getApplicationContext(), "Data Submitted Successfully",
Toast.LENGTH_LONG).show();
// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
List<Datas> datas = db.getAllDatas();
for (Datas d : datas) {
String log = "Id: "+d.getID()+" ,Item: " + d.getItem() + " ,Comment: " + d.getComment() + " ,Date: " + d.getDate() + ",Comment: " + d.getTime();
// Writing Contacts to log
Log.d("Item: ", log);
}
}
});
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
AlertDialogManager.java
public class AlertDialogManager {
/**
* Function to display simple Alert Dialog
* #param context - application context
* #param title - alert dialog title
* #param message - alert message
* #param status - success/failure (used to set icon)
* - pass null if you don't want icon
* */
String savedinput;
public void showAlertDialog(final Context context, String title, String message,
Boolean status)
{
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
//setting input
final EditText input = new EditText(context);
alertDialog.setView(input);
// saving input to a string
savedinput = input.getText().toString();
System.out.println(savedinput);
if(status != null)
// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
savedinput = input.getText().toString();
SharedPreferences prefs = context.getSharedPreferences("myprefs", 0);
SharedPreferences.Editor editor =prefs.edit();
editor.putString("KEY_SAVEDINPUT", savedinput);
editor.commit();
System.out.println("from class "+savedinput);
}
});
// Showing Alert Message
alertDialog.show();
}
String getItem()
{
return savedinput;
}
}
and here is my logcat, just for further clarification
02-01 13:48:43.372: I/System.out(897): main : firstexecute
02-01 13:48:46.942: W/KeyCharacterMap(897): No keyboard for id 0
02-01 13:48:46.942: W/KeyCharacterMap(897): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-01 13:48:49.532: I/System.out(897): from class secondexecute
You have several issues here.
You shouldn't read savedValue in onCreate, you should do it only when you actually use it. See #ρяσѕρєяK answer.
alert.showAlertDialog is non blocking. So after dialog is shown line System.out.println("main : " + savedInput); is executed. It doesn't wait for your input. So you should call some other action beside saving to shared preferences on dialog's ok buttonn. This action should invoke logic that should happen after user entered some text in the dialog.
Update
public void showAlertDialog(final Context context, String title, String message,
Boolean status, final Spinner spinner)
{
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
//setting input
final EditText input = new EditText(context);
alertDialog.setView(input);
// saving input to a string
savedinput = input.getText().toString();
System.out.println(savedinput);
if(status != null)
// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
savedinput = input.getText().toString();
// do whatever you want with spinner and savedInput here.
}
});
// Showing Alert Message
alertDialog.show();
}
onClick to show dialog:
alert.showAlertDialog(MainActivity.this, "Enter Item",
"Please enter the spinner item",
true, (Spinner) findViewById(R.id.spin1));
Update 2
Apparently you need to add new item to your spinner adapter. For this you can create list of all items and pass this list along with adapter to dialog. When user enters string and press OK button onClick method adds this string to the list and call notifyDataSetChanged to update UI:
Add this in MainActivity:
List<String> spinnerItems;
In onCreate:
spinnerItems = new ArrayList<String>();
adapter = enw ArrayAdapter<String>(this, 0, spinnerItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Pass spinnerItems and adapter to showAlertDialog:
alert.showAlertDialog(MainActivity.this, "Enter Item",
"Please enter the spinner item",
true, spinnerItems, adapter);
And finally add text to list and notify adapter:
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
savedinput = input.getText().toString();
spinnerItems.add(savedInput);
adapter.notifyDataSetChanged();
}
});
you will need get latest value on Button click instead of onCreate to get latest value from SharedPreferences as :
addbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialogManager alert = new AlertDialogManager();
alert.showAlertDialog(MainActivity.this, "Enter Item",
"Please enter the spinner item",
true);
savedinput= prefs.getString("KEY_SAVEDINPUT",""); //<<< get value here
System.out.println("main : " +savedinput);
}
});
or you will try to get value from onResume of Activity
This
savedinput = input.getText().toString();
is evaluated when it is run. Not when you call getItem(). The value you'll get is what it was, in this case, before the alert was displayed.
same logic goes for
savedinput= prefs.getString("KEY_SAVEDINPUT","");
I've extended Application and declared a public ArrayList there that I use to hold data (I know it's not as a Singleton in android should be, but it was the only thing I could make work).
The problem :
When I display my CreateNewObject dialog and press OK I get the following error.
Error:
10-30 10:20:19.069: E/AndroidRuntime(632): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class com.nuclear.gfr.adapter.PatientAdapter)]
My code:
public class NewPatientDialog extends DialogFragment {
private static final String TAG = "NUKClear";
OnNewPatientlistener listener;
private NewPatientDialog() {
}
public interface OnNewPatientlistener {
public void OnNewPatient(Patient newPatient);
}
public void addPatientAction(Patient patient) {
OnNewPatientlistener activity = (OnNewPatientlistener) getActivity();
activity.OnNewPatient(patient);
}
public static NewPatientDialog newInstance(String title) {
NewPatientDialog frag = new NewPatientDialog();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View newPatient = inflater.inflate(R.layout.new_patient_dialog,
null);
Log.d(TAG, "Creating dialog for new patientinput...");
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setView(newPatient)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
final EditText name = (EditText) newPatient
.findViewById(R.id.name_edit);
final EditText ssn = (EditText) newPatient
.findViewById(R.id.ssn_edit);
final EditText accnum = (EditText) newPatient
.findViewById(R.id.accnr_edit);
final Patient patient = new Patient(ssn.getText()
.toString(), name.getText().toString(),
new Study(SimpleDateFormat
.getDateInstance().toString(),
accnum.getText().toString()));
addPatientAction(patient);
DatabaseHelper db = new DatabaseHelper(
getActivity());
db.addPatientStudy(patient);
/*DataManager dm = new DataManager(getActivity()
.getBaseContext());
dm.addPatient(patient);*/
/*
* PatientManager pm = new
* PatientManager(getActivity
* ().getBaseContext()); pm.add(patient);
*/
// Interfacecallback
/*
* OnNewPatientlistener activity =
* (OnNewPatientlistener) getActivity();
* activity.OnNewPatient(patient);
*/
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
dismiss();
}
}).create();
}
}
Can anyone explain why this happens and how to circumvent this?
Adding...
runOnUiThread(new Runnable() {
public void run() {
GFRApplication.dPatients.add(newPatient);
}
});
...did the trick.