Results 1 to 3 of 3
  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Why this type casting?

    I am using an intent to start another activity, and making my intent carry some data as an extra to the newly created activity. I am following a tutorial to do that.
    Java Code:
    [COLOR=gray][FONT=Consolas]//Make the intent   This data is actually read from a text field in the first activity in the hierarchy, and carried to the other activity as an extra. so my code will be like:
    
    
    [/FONT][/COLOR]
    Java Code:
    [COLOR=#2B91AF]Intent[/COLOR] intent = [COLOR=#00008B]new[/COLOR] [COLOR=#2B91AF]Intent[/COLOR]([COLOR=#00008B]this[/COLOR], [COLOR=#2B91AF]SecondActivity[/COLOR].[COLOR=#00008B]class[/COLOR]);
    
    [COLOR=gray]/* Find the text field (view) which contains the data, and save it into a newly created text field (view of the same type)*/[/COLOR]
    
    [COLOR=#2B91AF]EditText[/COLOR] editText = ([COLOR=#2B91AF]EditText[/COLOR]) findViewById(R.id.iden1); [COLOR=gray]//******************************[/COLOR]
    
    [COLOR=gray]//Read that view's string data into a string called message[/COLOR]
    
    [COLOR=#2B91AF]String[/COLOR] message= editmessage.getText().toString();
    
    [COLOR=gray]//copy this message into the extra named extra_name, of intent.[/COLOR]
     [COLOR=#000000][FONT=Arial][FONT=Consolas]intent[/FONT][FONT=Consolas].[/FONT][FONT=Consolas]putExtra[/FONT][FONT=Consolas]([/FONT][FONT=Consolas]extra_name[/FONT][FONT=Consolas],[/FONT][FONT=Consolas] message[/FONT][FONT=Consolas]);[/FONT][/FONT][/COLOR]
    Java Code:
    [COLOR=#2B91AF][FONT=Consolas]EditText
    
    My question is from this statement:
    [FONT=Consolas] editText [/FONT][FONT=Consolas]=[/FONT][FONT=Consolas] [/FONT][FONT=Consolas]([/FONT][COLOR=#2B91AF][FONT=Consolas]EditText[/FONT][/COLOR][FONT=Consolas])[/FONT][FONT=Consolas] findViewById[/FONT][FONT=Consolas]([/FONT][FONT=Consolas]R[/FONT][FONT=Consolas].[/FONT][FONT=Consolas]id[/FONT][FONT=Consolas].[/FONT][FONT=Consolas]iden1[/FONT][FONT=Consolas]);[/FONT]
    [/FONT][/COLOR]

    My question is explicit casting i.e. (EditText). Why are we casting the EditText view (which was defined in layout.xml and was identified by android:id="@+id/iden1") returned by findViewById()to an EditText view again. The type of the view editText here and the one created in layout.xml is the same. So what is the point of this type-casting?
    Don't forget to smile :-)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: Why this type casting?

    Can you post the code again and remove all the markup tags. What is posted is very hard to read.

    Read the API doc for the method that is returning the reference. Its return type is a super class. You need to cast what is returned to the type that you know the reference to be so you can call its methods.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Why this type casting?

    Can you post the code again
    I can't find how to edit my post. More importantly, although I am grateful to you for answering, but I had reached an answer to this question, so there is no more a need to spend time/energy on it. Actually I was wrong in the posted question, I thought casting from subclass to superclass is not safe, and vice versa is safe. But actually casting from sub class to super class, i.e. using a super class object in place of a subclass object (implicit casting) is always safe.

    Thank you considering to answer my question.
    Don't forget to smile :-)

Similar Threads

  1. type casting
    By saidinu in forum New To Java
    Replies: 1
    Last Post: 10-21-2011, 01:09 PM
  2. Type casting
    By kiros88 in forum New To Java
    Replies: 17
    Last Post: 09-02-2010, 01:58 PM
  3. help with type casting.
    By ramsrocker in forum Java Applets
    Replies: 15
    Last Post: 02-26-2009, 11:28 PM
  4. type casting
    By alvations in forum New To Java
    Replies: 1
    Last Post: 10-13-2008, 07:07 PM
  5. Type Casting Help
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 02-07-2008, 12:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •