Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Monday, June 10, 2013

Dialog/orientation change memory leaks...

Well, today I kept battling a memory leak in my opening screen for Perspective when rotating the phone.  A quick search yielded plenty of references to orientation changes when rotating.  There were quite a few work arounds, but I found a simple one that really made it simple for me so I figured I'd share.

To fix the problem in my PopDialog class that extends a Dialog, I added a

static PopDialog pd=null;  

Then I added this just after the onCreate

  pd=PopDialog.this;
  MainActivity.opened=true;

and finally I added this method

 public void dismissing(){
if(pd.isShowing()){pd.dismiss();}

}

OK, that's the hard part.  Now in my main activity, I referenced it like this

 PopDialog popdialog;
 private static boolean opened=false;

after onCreate in that activity

popdialog=new PopDialog(this);

and finally in the onPause

@Override
protected void onPause() {
super.onPause();
if(opened){ popdialog.dismissing();}


}
I forgot, add this to the button you call the Dialog from because sometimes it doesn't call onCreate.

opened=true;  

This way when rotating, it always has the proper reference to the active dialog and calls to dismiss it destroying it.  I had to update it a bit by adding the opened in there.  This is won't let it call dismiss before the dialog is created.

Tuesday, April 16, 2013

Binary, bits, bytes, short, long, and integer.

Recently I took a class on networking and had never really put any thought into binary, but found it interesting.  As some know, a byte is 256 bits of information in the range of 0-255.  Below is considered an octet because it is 8 bits, yeah, 8 bits like your old NES.  Below is the breakdown of how it works.


Ok, now that we understand the byte, we can understand how to use a byte in java.  Still the same 8 bits of information, but a range of –128 to 127 for a total of 256.  Makes sense, let's continue.

Now, with that in mind, let's compare it to a short.  Yeah, I know, short isn't that common, but is 16 bit, or 2 octets.  This means it has a range of –32,768 to 32,767.  Below is the double octet that represents it.  Notice how the number keeps doubling in the next octet.



Since that makes sense now, it only makes sense that an integer being 32 bit, is basically 4 octets that will continue doubling until it reaches a range of  -2,147,483,648 to 2,147,483,647.

At this point you can understand that a 64 bit long has a range of –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

byte=–128 to 127;
short=–32,768 to 32,767
integer=-2,147,483,648 to 2,147,483,647
long=–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Wednesday, March 13, 2013

SQLite Cursor explained

As I started on my Soccer Stat Master app, I realized immediately that Shared Preferences would NOT be the way to go no matter how bad my illusions of grandeur were.  This led me to SQlite which went surprisingly smooth until I started messing with the cursors.  A lot of my pulls from the db were a little complex so I needed to really understand the cursor function.  My first dive used this.

Cursor allrows  = gameDB.rawQuery("SELECT * FROM "+ PLAYERTABLE, null);

This was fine and dandy, so I used this to query what I needed.  Below shows how to add to either an array or an arraylist just for reference.  Anyway, the point is the cursor is aimed at row 1 and row 3.  Seemed simple enough.


     if(allrows.moveToFirst()){
      int t;
            do{
                names.add(allrows.getString(1);
            name[t]=allrows.getString(1);  //NAME
                time[t]=allrows.getInt(3);  //TIME
                t++;
            }
            while(allrows.moveToNext());
        }



Now, here is where it got tricky for me mainly because I didn't understand the relation at first so I hope I can save someone else the headache.  For my next call I just wanted a single item.  Rather than pull the whole db just for that, I decided to use this.  



Cursor allrows  = gameDB.rawQuery("SELECT AGE FROM "+ PLAYERTABLE, null);

My thinking was that if I call allrows.getInt(2) it would get that row, but what didn't sink in was the cursor had already grabbed that row.  After repeated errors, it hit me.  The reason I was allowed to used the numbers earlier was because I was using the WHOLE db for cursor position.  A simple change to 0 would fix this problem because the cursor was honed in on a row.


     if(allrows.moveToFirst()){
       int t;
            do{
           
               age[t]=allrows.getInt(0);  //AGE
                t++;
            }
            while(allrows.moveToNext());
        }



Eventually I really needed to hone in on one by a variable added in.  At this point I was beginning to grasp what was going on.  Here are a few solutions for the same situation.  Also, maybe they can help you understand how the cursor works by comparing them.

Cursor allrows  = gameDB.rawQuery("SELECT AGE FROM "+ PLAYERTABLE, null);

   if(allrows.moveToPosition(cursorPos)){
       int t;
            do{
           
               age[t]=allrows.getInt(0);  //AGE
                t++;
            }
            while(allrows.moveToNext());
        }


This would be the same as..

Cursor allrows  = gameDB.rawQuery("SELECT * FROM "+ PLAYERTABLE, null);


   if(allrows.moveToPosition(cursorPos)){
    
                     
               age[t]=allrows.getInt(2);  //AGE
             
        }

or even this one which hones in immediately..


Cursor allrows  = gameDB.rawQuery("SELECT AGE FROM WHERE ID='"+cursorPos"'"+ PLAYERTABLE, null);



     if(allrows.moveToFirst()){
          do{
                          age[t]=allrows.getInt(0);  //AGE
                      }
            while(allrows.moveToNext());
      }






Hopefully this can help clear up cursors in sqlite.  Feel free to show your support by downloading my apps from the android,blackberry, or amazon market under A54studio.

A54studio on Amazon

A54studio on Play

A54studio on Blackberry