3

I call on a few EditTexts via "instanceOf" because I don't necessarily know the exact names / ids of them. When I do this to get the text, it works just fine. But when I try to setText() based on the local instanceOf EditText, it won't display the string I am passing it. Oddly enough, when I setText and then Log the getText, it logs it correctly -- it just won't physically display it.

I also tested setting the visibility to View.INVISIBLE or View.GONE just to see if i can interact with it at all, and it doesn't do anything.

Here is the code snippet that sets the text then logs it:

    LinearLayout mLayout = (LinearLayout) main_fields_holder.getChildAt(0);
         LinearLayout mLayout2 = (LinearLayout) mLayout.getChildAt(0);
         LinearLayout mLayout3 = (LinearLayout) mLayout2.getChildAt(1);
for(int i = 0; i < mLayout3.getChildCount(); i++){
                    View v = (View) mLayout3.getChildAt(i);
                    if (v instanceof EditText){
                        EditText e = (EditText) v;
                        e.setText("HELLO");

                        Log.v("USER TEXT", "Text is: " + e.getText().toString() + "!");

                        ;

                    }
} 

I should also mention that I do the same instanceOf method with button text and it won't display my setText either.

Is there some reason that a local instance of an editText (or button) cannot be manipulated?

By the way, the line Log.v("USER TEXT", "Text is: " + e.getText().toString() + "!"); DOES give me "HELLO!". I'm not even sure what that means for my problem.

The main layout file is:

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout    
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:background="#fff"
        xmlns:android1="http://schemas.android.com/apk/res/android">


    <ScrollView android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="fill_parent">
      <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" android1:focusable="false">

       <LinearLayout android:id="@+id/item_edit_linear" 
        android:layout_height="wrap_content"
        android:background="@drawable/backrepeat"
        android:layout_width="fill_parent"
        >
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/add_item_text" android:text="Add New Item" android:textStyle="bold" android:paddingLeft="15dip" android1:paddingTop="8dip" android1:textSize="12pt" android1:textColor="@color/blue"></TextView>
        </LinearLayout>
       <LinearLayout android1:layout_height="wrap_content" android1:layout_width="fill_parent" android1:orientation="vertical" android1:paddingLeft="15dip" android1:background="@drawable/borderlines" android1:paddingBottom="15dip" android1:paddingTop="10dip" android1:id="@+id/spinner_holder_layout">
           <TextView android1:layout_height="wrap_content" android1:layout_width="wrap_content" android1:paddingTop="5dip" android1:text="Item Name:" android1:textColor="@color/gray" android1:paddingBottom="5dip"></TextView>
           <EditText android1:layout_height="wrap_content" android1:layout_width="290dip" android1:id="@+id/edit_item_title">
               <requestFocus></requestFocus>
           </EditText>
           <TextView android1:layout_height="wrap_content" android1:layout_width="wrap_content" android1:paddingTop="5dip" android1:id="@+id/textView9" android1:text="Select Category:" android1:textColor="@color/gray" android1:paddingBottom="5dip"></TextView>
           <Spinner android1:layout_height="wrap_content" android1:layout_width="wrap_content" android1:prompt="@string/edit_item_category_spinner_text" android1:id="@+id/edit_item_category_spinner" android1:entries="@array/categoryList" android1:paddingBottom="10dip"></Spinner>
           <LinearLayout android1:layout_height="wrap_content" android1:layout_width="fill_parent" android1:orientation="vertical" android1:id="@+id/item_spinner_layout" android1:visibility="gone">
               <TextView android1:layout_height="wrap_content" android1:layout_width="wrap_content" android1:paddingTop="5dip" android1:id="@+id/textView10" android1:text="Select Item" android1:textColor="@color/gray" android1:paddingBottom="5dip"></TextView>
               <Spinner android1:layout_height="wrap_content" android1:layout_width="wrap_content" android1:prompt="@string/edit_item_item_spinner_text" android1:paddingBottom="10dip" android1:id="@+id/edit_item_spinner"></Spinner>
           </LinearLayout>
       </LinearLayout>
       <LinearLayout android1:layout_height="wrap_content" android1:layout_width="fill_parent" android1:orientation="vertical" android1:id="@+id/editItemFields">

       </LinearLayout>
       <LinearLayout android1:layout_marginLeft="15dip" android1:layout_width="fill_parent" android1:layout_height="wrap_content" android1:id="@+id/date_time_container" android1:visibility="gone">
           <LinearLayout android1:layout_width="wrap_content" android1:layout_height="fill_parent" android1:id="@+id/linearLayout7" android1:orientation="vertical">
               <TextView android1:layout_width="fill_parent" android1:id="@+id/dep_date_text" android1:textColor="@color/gray" android1:layout_marginBottom="10dip" android1:text="Departure Date:" android1:gravity="right|center" android1:layout_height="36dip"></TextView>
               <TextView android1:layout_width="fill_parent" android1:id="@+id/dep_time_text" android1:textColor="@color/gray" android1:layout_marginBottom="9dip" android1:text="Depature Time:" android1:gravity="right|center" android1:layout_height="36dip"></TextView>
               <TextView android1:layout_width="fill_parent" android1:id="@+id/arr_date_text" android1:textColor="@color/gray" android1:layout_marginBottom="9dip" android1:text="Arrival Date:" android1:gravity="right|center" android1:layout_height="36dip"></TextView>
               <TextView android1:layout_width="fill_parent" android1:id="@+id/arr_time_text" android1:textColor="@color/gray" android1:layout_marginBottom="9dip" android1:text="Arrival Time:" android1:gravity="right|center" android1:layout_height="36dip"></TextView>
           </LinearLayout>
           <LinearLayout android1:layout_marginLeft="5dip" android1:layout_width="wrap_content" android1:layout_height="fill_parent" android1:id="@+id/linearLayout8" android1:orientation="vertical">
               <Button android1:layout_width="wrap_content" android1:layout_height="45dip" android1:id="@+id/departure_date" android1:minWidth="125dip" android1:text="Button"></Button>
               <Button android1:layout_width="wrap_content" android1:layout_height="45dip" android1:id="@+id/departure_time" android1:minWidth="125dip" android1:text="Button"></Button>
               <Button android1:layout_width="wrap_content" android1:layout_height="45dip" android1:id="@+id/arrival_date" android1:minWidth="125dip" android1:text="Button"></Button>
               <Button android1:layout_width="wrap_content" android1:layout_height="45dip" android1:id="@+id/arrival_time" android1:minWidth="125dip" android1:text="Button"></Button>
           </LinearLayout>
       </LinearLayout>

        <LinearLayout android:layout_width="fill_parent" android:id="@+id/editItemLinearLayout" 
        android:layout_height="fill_parent" android:orientation="vertical" android1:paddingLeft="15dip" android1:visibility="gone">
        <TextView android:text="Cost" android:id="@+id/textView1" 
            android:layout_width="wrap_content" android:layout_height="wrap_content">
        </TextView>
        <EditText android:layout_height="wrap_content" 
            android:layout_width="fill_parent" android:id="@+id/edit_item_cost">
        </EditText>
        <TextView android:text="Contact Phone" android:id="@+id/textView2" 
            android:layout_width="wrap_content" android:layout_height="wrap_content">
        </TextView>
        <EditText android:layout_height="wrap_content" 
            android:layout_width="fill_parent" android:id="@+id/edit_item_phone">
        </EditText>
        <TextView android:text="Carrier" android:id="@+id/textView3" 
            android:layout_width="wrap_content" android:layout_height="wrap_content">
        </TextView>
        <EditText android:layout_height="wrap_content" 
            android:layout_width="fill_parent" android:id="@+id/edit_item_carrier">
        </EditText>
        <TextView android:text="Names" android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        <EditText android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/edit_item_names">
        </EditText>
        <TextView android:text="Departure Location" android:id="@+id/textView5" 
            android:layout_width="wrap_content" android:layout_height="wrap_content">
        </TextView>
        <EditText  android:layout_height="wrap_content" 
            android:layout_width="fill_parent" android:id="@+id/edit_item_departure_location">
        </EditText>
        <TextView android:text="Arrival Location" android:id="@+id/textView6" 
            android:layout_width="wrap_content" android:layout_height="wrap_content">
        </TextView>
        <EditText android:layout_height="wrap_content" 
            android:layout_width="fill_parent" android:id="@+id/edit_item_arrival_location">
        </EditText>
        <TextView android:id="@+id/textView7" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Date"></TextView>
        <Button android:text="Button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/edit_item_reminder_date"></Button>
        <TextView android:id="@+id/textView8" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Time"></TextView>
        <Button android:text="Button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/edit_item_reminder_time"></Button>
        </LinearLayout>
        <RelativeLayout android1:layout_width="fill_parent" android1:layout_marginTop="50dip" 
        android1:id="@+id/relativeLayout1" android1:layout_height="fill_parent">
            <Button android1:id="@+id/edit_item_confirm" android1:layout_width="wrap_content" android1:layout_height="wrap_content" android1:background="@drawable/savebutton" android1:layout_alignParentBottom="true"></Button>
            <Button android1:width="159dip" android1:background="@drawable/cancelbutton" android1:layout_width="wrap_content" android1:id="@+id/item_creation_cancel" android1:layout_height="wrap_content" android1:layout_alignParentRight="true" android1:layout_alignParentBottom="true"></Button>
        </RelativeLayout>

        </LinearLayout>

    </ScrollView>

    </LinearLayout>

Through code, I inflate the editItemFields Layout with the following:

<LinearLayout android1:paddingLeft="15dip" android1:layout_width="fill_parent" 
android1:background="@color/white" android1:id="@+id/flight_edit_container" 
android1:orientation="vertical" android1:layout_height="fill_parent" 
xmlns:android1="http://schemas.android.com/apk/res/android" android1:paddingTop="10dip">
    <LinearLayout android1:layout_height="wrap_content" android1:layout_width="fill_parent" android1:id="@+id/linearLayout6">
        <LinearLayout android1:layout_height="fill_parent" android1:layout_width="wrap_content" android1:id="@+id/linearLayout7" android1:orientation="vertical">
            <TextView android1:id="@+id/TextView02" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="10dip" android1:text="Airline:"></TextView>
            <TextView android1:text="Flight #:" android1:id="@+id/TextView05" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="10dip"></TextView>
            <TextView android1:text="Gate:" android1:id="@+id/TextView04" android1:textColor="@color/gray" android1:gravity="right|center" android1:layout_width="100dip" android1:layout_height="36dip" android1:layout_marginBottom="10dip"></TextView>
            <TextView android1:id="@+id/TextView09" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:text="Departure Location:" android1:layout_height="36dip" android1:layout_marginBottom="10dip"></TextView>
            <TextView android1:text="Departure Airport:" android1:id="@+id/TextView14" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="10dip"></TextView>
            <TextView android1:text="Confirmation #:" android1:id="@+id/TextView12" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
            <TextView android1:gravity="right|center" android1:text="Destination:" android1:id="@+id/TextView08" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
            <TextView android1:text="Arrival Airport:" android1:id="@+id/TextView15" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
            <TextView android1:text="Cost:" android1:id="@+id/TextView09" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
            <TextView android1:text="Class:" android1:id="@+id/TextView08" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
            <TextView android1:text="Seat:" android1:id="@+id/TextView11" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
            <TextView android1:text="Travelers:" android1:id="@+id/TextView12" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
            <TextView android1:text="Memo:" android1:id="@+id/TextView13" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
        </LinearLayout>
        <LinearLayout android1:layout_height="fill_parent" android1:layout_width="wrap_content" android1:id="@+id/linearLayout8" android1:orientation="vertical" android1:layout_marginLeft="5dip">
            <EditText android1:layout_width="190dip" android1:layout_height="45dip" android1:id="@+id/flight_airline"></EditText>
            <EditText android1:layout_width="190dip" android1:id="@+id/flight_number" android1:layout_height="45dip"></EditText>
            <EditText android1:layout_width="190dip" android1:id="@+id/flight_gate" android1:layout_height="45dip"></EditText>
            <EditText android1:layout_width="190dip" android1:id="@+id/flight_deplocation" android1:layout_height="45dip"></EditText>
            <EditText android1:layout_width="190dip" android1:id="@+id/flight_depairport" android1:layout_height="45dip"></EditText>
            <EditText android1:layout_width="190dip" android1:id="@+id/flight_confirmation" android1:layout_height="45dip"></EditText>
            <EditText android1:layout_height="45dip" android1:layout_width="190dip" android1:id="@+id/flight_destination"></EditText>
            <EditText android1:layout_height="45dip" android1:layout_width="190dip" android1:id="@+id/flight_arrairport">
            </EditText>
            <EditText android1:layout_width="190dip" android1:layout_height="45dip" android1:id="@+id/flight_cost"></EditText>
            <EditText android1:layout_width="190dip" android1:layout_height="45dip" android1:id="@+id/flight_class"></EditText>
            <EditText android1:layout_width="190dip" android1:layout_height="45dip" android1:id="@+id/flight_seat"></EditText>
            <Button android1:text="Button" android1:minWidth="125dip" android1:id="@+id/flight_travelers" android1:layout_width="wrap_content" android1:layout_height="45dip"></Button>
            <EditText android1:layout_width="190dip" android1:id="@+id/flight_memo" android1:layout_height="45dip"></EditText>
        </LinearLayout>
    </LinearLayout>
           </LinearLayout>

I basically drill down into the layouts until i am at the linear layout that holds my editTexts.

Finally, here is the code snippets that inflate the layout:

main_fields_holder = (LinearLayout) findViewById(R.id.editItemFields);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.flight_fields,
                        null);
                 main_fields_holder.addView(view);

UPDATE: Just to test, I added this line to see if i can hard reference it by id:

EditText a = ((EditText)findViewById(R.id.flight_airline));
            a.setText("HELLO");

And it STILL won't show up. Could the fact that I inflate the layout that holds these items have anything to do with it? Basically when a spinner item is selected, a layout is inflated and I try to set the Text of the items in that layout, but to no avail. Btw, I can also still trace out the value of the EditText after setting it, but the bugger just won't physically show up.

Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69
Nick
  • 33
  • 1
  • 5

6 Answers6

3

There seem to be very subtle but important differences when retrieving Views with findViewById().

If you call findViewById(R.id.yourEditText) "natively" e.g. in a top-level-line of the onResume() Method, the EditText seems to behave correctly, setText(...) shows the new Value in the display.

But if you put the Activity into a member variable of another Object (e.g. some POJO), and you do this:

editText = pojo.getActivity().findViewById(R.id.EditText);

you still will find the right an indistinguishable EditText-Object, but setText() doesn't work anymore. I don't know why.

I spent a lot of hours to find this (API Level 7-Project). Huuuh!!!


(Oooops - forget it. I didn't have a POJO, but some wrong Map-caching which gave me an old Activity-Object)

Trung Nguyen
  • 7,442
  • 2
  • 45
  • 87
hmms
  • 31
  • 2
3

FYI I figured this out. For whatever reason, the app needed a moment to go through all of the functions that grab the information and set them, so I added a post handler:

handler.postDelayed(new Runnable() { public void run() { populateFields(item); } }, 500);

This 1/2 second wait allowed the information to be displayed properly. Thanks for everyone's answers

1

Try doing:

final EditText a = ((EditText)findViewById(R.id.flight_airline));
a.post(new Runnable() {

     @Override
     public void run() {
         a.setText("HELLO");
     }
});

Which is a much better solution than the one currently marked as accepted. This will post it to the UI when it's actually ready, instead waiting around for 1/2 second.

Joakim Engstrom
  • 6,243
  • 12
  • 48
  • 67
1

It sounds very much like you're not getting the EditText that you think you are. If you give some more detail, maybe we can help you find the right one.

Update:

So it seems that you do actually know the IDs, the problem is that they're not unique. The solution is to start from a known point in the view hierarchy where you know they're unique. For example, after you inflate your editItemFields layout, you have a reference to the root of that view hierarchy in your view variable. You can then do something like:

EditText flightNumberEditText = (EditText) view.findViewById(R.id.flight_number);

And you'll get the flight_number EditText for that particular item.

kabuko
  • 36,028
  • 10
  • 80
  • 93
  • I considered that, but the problem is that I have a layout with a bunch of EditText's, and NONE of them show any of the text that is suppose to be displayed. and it is obviously finding at least some of the edittexts because i am not getting a null pointer exception. The layout is added via a layout inflator though. Would that change anything? – Nick Sep 01 '11 at 18:31
  • Edit your post to include the layout file and the code where you instantiate/inflate mLayout3, and I'll take a look. – kabuko Sep 01 '11 at 18:34
  • Ahh but the problem with that is I have multiple views that I can inflate. I just posted one of the views. I am more or less trying to dynamically set the text of the inflated layout's edittexts, so I can't use findViewById – Nick Sep 01 '11 at 18:58
  • Right, but that's what I'm saying too. :) If you can get to the root of each, you can start from that root view and call `findViewById` on that root view and it'll search under that view's hierarchy instead of top level. – kabuko Sep 01 '11 at 18:59
  • If i were to do that, I would have to have a seperate findViewById for all 109 editTexts wouldn't I? I was hoping to accomplish this in a dynamic way (like above) in which I don't search for a specific id – Nick Sep 01 '11 at 19:40
  • To clarify, you have 100+ `EditText`s and you want to set them all to the same value? I mean, it seems to me that you have stuff like flight number and flight gate which would be different values, right? – kabuko Sep 01 '11 at 20:24
  • I don't want them all to be the same value. I have a system set up to grab database values as I need them. I am more concerned with my initial problem of how / why i can't reference the edittexts through the instanceOf method. I just put the setText to "hello" as an example. Vladimir sounded like he was on to something when he suggested that perhaps I was getting a copy of the edittext and therefore could not set the text of the original, but I still have no idea how can i reference the original if that is indeed the problem – Nick Sep 02 '11 at 12:43
  • Check out my latest update in my post. Even using FindViewById won't get the text to show up. Now I am thoroughly confused – Nick Sep 02 '11 at 14:27
0

Set the ID of the EditText when you create it when you should be able to work with it.

e.setId(15); //15 is any positive integer

On the opposite you can always try

e.getEditableText();

Cistoran
  • 1,587
  • 15
  • 36
  • 54
  • Thank you for the suggestion, but that doesn't appear to help any. I added e.setId(15) after "EditText e = (EditText) v" with no result. I should also mention that I do the same instanceOf method with button text and it won't display my setText either – Nick Sep 01 '11 at 18:20
0

try

((EditText) v).setText("HELLO");
Vladimir
  • 9,683
  • 6
  • 36
  • 57
  • 1
    Thank you for the suggestion. This doesn't appear to solve the problem either. The thing that boggles me is that in a different section of my class, i am doing the exact same thing except i am using getText() and it is getting the correct text. – Nick Sep 01 '11 at 18:25
  • 1
    i'm not sure if getChildAt() returns refference to the View or copy of it. so if it returns copy you will be able to getText(), but setting it will have no effect, since it is not the view in your mLayout3 but a copy of it - that's why you get correct log – Vladimir Sep 01 '11 at 18:30
  • That makes sense. Is there any way to set the text of the original and not the copy without hard coding the exact findViewById id? – Nick Sep 01 '11 at 18:33
  • i was wrong, it seems to be returning refference, cause `LinearLayout mainLL = (LinearLayout)findViewById(R.id.main_LL); View testV = (View)mainLL.getChildAt(0); ((TextView)testV).setText("test"); Log.v("out: ", ((TextView)mainLL.getChildAt(0)).getText().toString());` works just as intended. you should follow kabuko's advice and make sure you are getting EditText's that you really need – Vladimir Sep 01 '11 at 18:59
  • I am getting all of the EdiTexts that i need. I get every one and use every one. The problem is the only way i can get them is to iterate through all of the layout children until i find an editText, and I can't seem to reference it correctly to setText – Nick Sep 01 '11 at 19:09