Android Programming Issue - checkedId

Discussion in 'Programming General' started by symphony888, Jul 15, 2014.

Android Programming Issue - checkedId
  1. Unread #1 - Jul 15, 2014 at 10:39 PM
  2. symphony888
    Joined:
    May 30, 2010
    Posts:
    2,464
    Referrals:
    4
    Sythe Gold:
    0

    symphony888 Request a PM before trading! <3 Sythe.Org
    Banned

    Android Programming Issue - checkedId

    I'm clueless right now. I'm working on my project and I got some references for some part of my project and while I was trying my best to understand the code. I just can't seem to understand 1 part of it which doesn't allow me to proceed.

    I hope someone kind enough can help me out and give me an explanation on this issue.

    Anyone can explain to me about the part positionData = (byte) (checkedId - position1Button.getId());

    What is the real purpose of this? I don't understand why it has a minus sign there and also how does the (byte) thingy works.

    Code:
    private byte positionData;
       class PositionRadioGroupListener implements android.widget.RadioGroup.OnCheckedChangeListener{
    	      
    		@Override
    		public void onCheckedChanged(RadioGroup group, int checkedId) {
    			positionData=(byte) (checkedId - position1Button.getId());
    			imageChange();
    		}//onCheckedChanged
       }
     
  3. Unread #2 - Jul 18, 2014 at 2:13 AM
  4. 70i
    Joined:
    Jan 11, 2014
    Posts:
    462
    Referrals:
    0
    Sythe Gold:
    174

    70i Forum Addict
    Banned

    Android Programming Issue - checkedId

    This method is "Called when the checked radio button has changed."
    http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html

    What object is position1Button? It's kind of hard to say what that line is without that.

    The (byte) thing is called casting. All that's really doing is changing an Integer to a byte.

    For example you could do
    int a =5;
    byte b = (byte) a;
    or vice versa
    byte a =5;
    int b = (Integer) a;
     
  5. Unread #3 - Jul 18, 2014 at 4:14 AM
  6. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    Android Programming Issue - checkedId

    I have never dealt with a byte in java so take this information with a grain of salt; it is just a guess. I also have never done android programming. Does the android program deal with a lot of position data? How often is this method called? As 70i mentioned the (byte) is casting what proceeds it to a byte (1 byte = 8 bits). My guess is that the program is trying to save on memory allocated under the hood (especially if a lot of these "position data" are used. If checkedId - position1Button.getId() represents an int (i'm assuming), recall that an integer can hold 32 bits (4 bytes). However if the data you are actually storing never falls out of the range -128 to 127, it is more efficient to cast it to a byte to save on memory under the hood. A byte can hold 8 bits.

    Just keep in mind when creating large programs that the more complex a program gets, it usually means the more efficient your coding NEEDS to be. It isn't that big of a deal to save a few bytes, but when a method is getting called constantly, you need to be efficient with memory allocation or it could eventually crash your program (I think it is a OutOfMemoryException that is thrown in java).
     
  7. Unread #4 - Jul 27, 2014 at 3:28 AM
  8. symphony888
    Joined:
    May 30, 2010
    Posts:
    2,464
    Referrals:
    4
    Sythe Gold:
    0

    symphony888 Request a PM before trading! <3 Sythe.Org
    Banned

    Android Programming Issue - checkedId


    Thanks for the explanation :) I finally got what it is about.
     
< Looking for RSPS help | C++ Question >

Users viewing this thread
1 guest


 
 
Adblock breaks this site