# [Q] Alert Dialog from OnListItemClick?



## SkullOne (Jul 9, 2011)

What's the easiest way to get an alert dialog to pop up after an item from a ListView has been clicked? I've tried a number of things I found on Google and none of them seem to work.

What I need is a way to get a confirmation dialog box to pop up confirming that the user wants to delete the file they clicked and then either allow the user to hit OK to delete the file or hit Cancel to abandon the deletion.

I've tried setting up a long click listener for the ListView to no avail (I'd still prefer this method if somebody could point me to a good tutorial on how to make it work). So I created a separate class to handle file management and it works with deleting the file but I can't figure out how to get a dialog confirmation to pop up.

Code for the import class is here:

https://github.com/skullone/android_firewall/blob/master/src/com/jtschohl/androidfirewall/RulesDialog.java

The new class hasn't been pushed to github yet but it's more or less the same as RulesDialog.java just with a few things changed in order to delete the file instead of importing the file.

Any help would be greatly appreciated.



Thanks!


----------



## yarly (Jun 22, 2011)

I would extend dialog fragment, also import the v4 support so it can work on any Android version. Also your tabbing/formatting is all over the place in your link. Try to be consistent so it's easier to read. Example of how I format my code to get an idea of what I mean (don't have to follow my bracket style of course, as I deviate from Android's due to adhering to a standard before Android's existence). Helps to also break really long lines up into seperate lines where it makes sense to break them (usually at method calls or something). Also, you shouldn't be suppressing warnings (fix them instead) and it's a good idea to keep the override statements there for verbosity, even if they're not needed for compile.

Looking at your code for the shared preferences stuff, I was wondering why are you are manually reading them from a file when Android provides a way to store and retrieve preferences easily. http://stackoverflow...and-edit-values

http://developer.and...ogFragment.html
http://android-devel...gfragments.html

If you have any other questions, feel free to ask.


----------



## JBirdVegas (Jun 11, 2011)

You made a builder but never called .build() which returns an AlertDialog you can then call show().

https://github.com/skullone/android_firewall/blob/master/src/com/jtschohl/androidfirewall/RulesDialog.java#L96

Before you return make that line something like
builder.build().show();


----------



## SkullOne (Jul 9, 2011)

yarly said:


> You made a builder but never called .build() which returns an AlertDialog you can then call show().
> 
> https://github.com/s...Dialog.java#L96
> 
> ...


Well aren't I lucky that I left that code in there. LOL

I didn't realize I had that in there when I commit the code for my Null Pointer fix.







Thanks! I'll add that in and work from there.


----------



## yarly (Jun 22, 2011)

Are you coding in an IDE? It mostly auto formats my code for me (rest can be done by using the shortcut keys for formatting), though it's also kind of habitual now as well. Either Eclipse or Intellij IDEA will do it if set up to your preferences.


----------



## SkullOne (Jul 9, 2011)

Yes. Using Eclipse. I'll look into the auto format to help fix the code. Thanks!

Sent from my Galaxy Nexus using Tapatalk 2


----------



## SkullOne (Jul 9, 2011)

Many thanks to you to both. I now have the file deletions working properly in my new class.


----------



## JBirdVegas (Jun 11, 2011)

Here is a utility I wrote in java to reformat the tabs to android. It requires java 7

https://github.com/JBirdVegas/java/blob/java7/src/TabsToWhitespace.java

Its even included in aokp see the README for usage. https://github.com/AOKP/vendor_aokp/tree/jb-mr1/tools/java


----------



## JBirdVegas (Jun 11, 2011)

I was just thinking since the Android SharedPreferences are not thread safe manually reading would allow different threads access... If it doesn't throw an IOException


----------

