0

from what I've read so far, it's bad practice/impossible to loop and have an AlertDialog pop up each time. But I haven't found anything alternatives to satisfy that functionality.

The example is: get a cursor from a DB and loop through each item. while looping through do a comparison on a text field. If they don't equal each other, show an alert to decide what to do ie. append, overwrite or skip the text.

thanks

Nova103
  • 23
  • 3

2 Answers2

2

You could also collect the information and use a list view instead that offers options per conflict. The popup way will drive people mad and nobody is going to use it more than once.

meredrica
  • 2,563
  • 1
  • 21
  • 24
-1

Loop through the cursor to read each element (Here I am considering it to be a list). If the trigger condition is met, set boolean WAITING to true, call showDialog() and finally call a waiting loop. This waiting loop will ensure that the parent for loop waits untill the user responds to the inflated dialog from the showDialog().

 for(int count =0 ; count<100 ; count ++)
    {
     if(List.get(count).ID != InputValue)
     {
     WAITING=true;
     showDialog(List.get(count).ID , InputValue);
     while(WAITING);
     }
    }

In showDialog() first create the Dialog and setCancelable() to false so that user has to click on one of the three buttons(append, overwrite or skip) to disable the dialog. Then handle click events for these three, do the required DB operation and finally set WAITING=false and Hide the dialog. This will resume the parent for loop.

showDialog(String DB_Value, Input)
{
Show Dialog with option Buttons and Set dialog.setCancelable(false);
On click of any of one of the three buttons(append, overwrite or skip)
  1. do the required DB action
  2. hide the dialog
  3. WAITING= false
}

Hope this helps you!

Parth Kapoor
  • 1,494
  • 12
  • 23
  • Many problems with this solution. The first, you're essentially asking the OP to block the UI thread in an infinite `while` loop. If the user does not respond, an ANR will occur. The second is that `showDialog` will immediately set `WAITING` to `false` after showing the dialog, so the `while` code would not even run. There are several other problems with this answer, you might want to revise it. – ugo Apr 15 '14 at 14:44
  • @Ugo Blocking the UI thread is not recommended, generally. However the nature of this problem is such that it has to blocked so as to show concurrent alerts to allow the user. Moreover, The first problem you mentioned, can be resolved by running this solution on a separate thread, may be an async task. Second concern is wrongly interpreted.WAITING will only be set to false on click of one of the 3 buttons, not otherwise. What are your thoughts on this? Any different approach that you can suggest for the same? – Parth Kapoor Apr 15 '14 at 17:04
  • In Android, when a Dialog is called, code execution continues and doesn't wait for user input. So after 'Show Dialog with options', the rest of the code will execute whether the user clicks a button or not, so WAITING will be set to false, regardless. I don't have any solution to the problem using Dialogs, I don't even recommend using Dialogs to solve this. However, I suggest the OP looks into @meredrica's solution. – ugo Apr 15 '14 at 18:17
  • @Ugo "the rest of the code will execute whether the user clicks a button or not"....I do not buy this. Do you mean if I were to show a toast from Dilog Button's onClickEvent, then regardless of the code being mentioned in onClick, the toast will simply be shown at the time of dialog creation? Well, with due respect, I do not think so. Can you please satisfy my curiosity on this, please. – Parth Kapoor Apr 16 '14 at 02:27
  • Take a look at [this answer](http://stackoverflow.com/a/2029128/779983) by Romain Guy. It explains how Android does not have synchronous dialogs – ugo Apr 16 '14 at 02:30
  • 1
    Ack! It seems we had a misunderstanding. I thought your pseudocode showed code execution blocking AFTER the `Showdialog` part. My apologies. Please edit the answer and make it clearer so I could correct my comments. – ugo Apr 16 '14 at 02:36
  • I do not wish to go into such a discussion on what is recommended and what is not. I simply wish to provide the solution to the problem that has been expressed here, I do not wish to change the whole context of the problem. Its like some one asks you the way to Pluto and you end up telling him it is not possible and not recommended, you should visit moon instead, simply because it is common and recommended! Well, I know there are lots of different approaches which can achieve the requirement here. But what if simply wants to do it with dialogs? – Parth Kapoor Apr 16 '14 at 02:43