1. Download our Official Android App: Forums for Android!

Apps Code in try {} doesn't seem to be running?

Discussion in 'Android Development' started by icydash, Jun 12, 2011.

  1. icydash

    icydash Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    12
    Joined:
    Jun 8, 2011

    Jun 8, 2011
    12
    0
    15
    Hey guys. So I have some code that, upon clicking an item on a list, creates an AlertDialog which asks you if you'd like to delete the item. For some reason, when you hit the "yes" button, the code doesn't seem to be deleting the user from the sql database. There are no errors or anything, though, in compiling or when i run the program.

    Strangely, actually, the code in the "try {}" doesn't seem to be running at all. I tried putting a Toast in there, and it doesn't come up, which leads me to think the try {} code isn't executing.

    Any ideas? The relevant parts of the code is below, but i can give you my full code if necessary.

    Code (Text):
    1. lv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
    2.         {
    3.             @Override
    4.             public boolean onItemLongClick(AdapterView<?> parent, View view,
    5.                 int position, long id)
    6.             {                
    7.               //dialogue
    8.               final CharSequence name = ((TextView) view).getText();
    9.               AlertDialog.Builder builder = new AlertDialog.Builder(uNear.this);
    10.               builder.setMessage("Select an Option")
    11.                      .setPositiveButton("User Information", new DialogInterface.OnClickListener() {
    12.                          public void onClick(DialogInterface dialog, int id) {
    13.                              //----------------alert "user info"
    14.                          }
    15.                      })
    16.                      .setNegativeButton("Delete User", new DialogInterface.OnClickListener() {
    17.                          public void onClick(DialogInterface dialog, int id) {
    18.                              //alert "are you sure?"
    19.                
    20.                              AlertDialog.Builder subbuilder1 = new AlertDialog.Builder(uNear.this);
    21.                              subbuilder1.setMessage("Are you sure you want to delete "+name+"?")
    22.                                     .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    23.                                         public void onClick(DialogInterface dialog, int id) {
    24.                                            
    25.                                             //--------------------DATABASE-----------------------    
    26.                                              SQLiteDatabase myDB = null;
    27.                                              
    28.                                              try {
    29.                                                     myDB = openOrCreateDatabase("uNearDatabase", MODE_PRIVATE, null);
    30.                                                     myDB.execSQL("DELETE * FROM uNearTable WHERE Field1='"+name+"';");
    31.                                                     Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
    32.                                               }
    33.                                               catch(Exception e)
    34.                                               {
    35.                                                    Log.e("Error", "Error", e);
    36.                                               }
    37.                                               finally
    38.                                               {
    39.                                                   if (myDB != null)
    40.                                                        myDB.close();
    41.                                               }
    42.                                              //-------------------------------------------------------------  
    43.                                            
    44.                                             //Toast.makeText(getApplicationContext(), name+" deleted", Toast.LENGTH_SHORT).show();
    45.                                         }
    46.                                     })
    47.                                     .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    48.                                         public void onClick(DialogInterface dialog, int id) {
    49.                                              
    50.                                         }
    51.                                     });
    52.                              subbuilder1.show();
    53.                            //end secondary alert
    54.                          }
    55.                      });
    56.               builder.show();
    57.               //end dialogue
    58.             return true;
    59.             }
    60.           });
     

    Advertisement

  2. JiMMaR

    JiMMaR Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    363
    Joined:
    Jun 12, 2010

    Jun 12, 2010
    363
    57
    53
    maybe your method to open the db is not correct
    try debugging , set a bullet point on the SQLiteDatabase initialization line and click on the debug icon
    walk with it and see what's happening exactly .. if it goes inside try then to catch, then you are doing something wrong
    you can also check the log in DDMS to see what kind of exception is being thrown

    if that's too much .. then put a toast inside the catch block
    if that one gets executed , then there is something wrong with the try block , see what you are doing

    I'm assuming the part where u say that there is no errors or anything actually means that you are not getting a force close (crash) , you won't get that if you are catching the exception


    Edit: I'm not an SQL expert , but shouldn't the line
    Code (Text):
    1.  myDB.execSQL("DELETE * FROM uNearTable WHERE Field1='"+name+"';");
    be
    Code (Text):
    1.  myDB.execSQL("DELETE * FROM uNearTable WHERE Field1="+name);
    ?
     
  3. icydash

    icydash Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    12
    Joined:
    Jun 8, 2011

    Jun 8, 2011
    12
    0
    15
    Thanks so much for your response. This is my first android program, so bear with me while I ask some really basic questions lol. What is the DDMS and how do I check the log there?

    I figured out the problem. It doesn't like the * in the SQL statement for some reason.
     
  4. icydash

    icydash Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    12
    Joined:
    Jun 8, 2011

    Jun 8, 2011
    12
    0
    15
  5. JiMMaR

    JiMMaR Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    363
    Joined:
    Jun 12, 2010

    Jun 12, 2010
    363
    57
    53
    I'm assuming you are using eclipse
    when you downloaded the ADT plugin for eclipse , you can also check something called DDMS
    if you enable it in eclipse , you should get a tab on the top right that says DDMS
    that'll show you the app log and you can also access the data files [pull and push files including databases] ,take screenshots, and simulate gps

    edit: pressing on the thanks button is appreciated :p
     
    alostpacket likes this.
  6. alostpacket

    alostpacket Over Macho Grande?
    Rank:
    None
    Points:
    513
    Posts:
    7,972
    Joined:
    Nov 29, 2009

    Nov 29, 2009
    7,972
    3,603
    513
    Android App Developer
    NY
    Havent looked too closely at this but for reference, SQL statements should be built with a bind, not by manually constructing a string. When you manually contruct a string you open yourself up to SQL injection attacks.

    This is one of those things I'm still going back and fixing lots of old code for because I never took the time to learn it correctly the 1st time, so I thought I would point it out :)

    Also I suspect this is a scope issue.

    openOrCreateDatabase() is not a global method you can call from onClick()


    I would expect the error to look something like:

    You need to call that method on something that is a subclass of Context (such as an Activity).

    Whenever I create onClick() functions I always call out to a different private or protected method to make the scope clear and code more readable.

    For example:
    Code (Text):
    1.  
    2. myButton.setOnClickListener( new OnClickListener()  
    3. {    
    4.     @Override public void onClick( View v )    
    5.     {
    6.          handleClick( v );
    7.     }
    8. });
    9.  
    10. protected void handleClick( View v )
    11. {
    12.    //do something here, if this method is in the activity
    13.    // then you can call the methods of the activity directly
    14. }
    15.  
    hth
     
    JiMMaR likes this.
  7. JiMMaR

    JiMMaR Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    363
    Joined:
    Jun 12, 2010

    Jun 12, 2010
    363
    57
    53
    why not define the onClickListener as a saperate object ? instead of calling a function from inside ? I've always done that, doesn't exactly look pretty .. but I think it looks less uglier than defining a new listener inside the onClick function

    ps: thanks for the thanks :p
     
  8. alostpacket

    alostpacket Over Macho Grande?
    Rank:
    None
    Points:
    513
    Posts:
    7,972
    Joined:
    Nov 29, 2009

    Nov 29, 2009
    7,972
    3,603
    513
    Android App Developer
    NY

    Yes, that's actually how I generally do it too, for readability. Not sure why I wrote it that way, was supposed to be a fast response heh.

    Edit: I should note though, I'd still call out of the View.OnClickListener object to make the scope more obvious and/or if I needed access to the Application/Activity Context.
     

Share This Page

Loading...