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.

有帮助吗?

解决方案

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

其他提示

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)

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.

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.

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();

try

((EditText) v).setText("HELLO");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top