简体   繁体   中英

TextView doesn't get updated

I'm facing an odd problem here. I'm just trying to set a value to a TextView in a Fragment. Problem is that it doesn't get updated at all. The color doesn't change too. The predefined text "test" is shown though, so I can rule out any layout related issues. Any ideas on this? Thanks !!

fragment_class.xml

    <?xml version="1.0" encoding="utf-8"?>
         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical" >

         <TextView
         android:id="@+id/classdetail"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="test"
         android:textColor="#000" />

    </RelativeLayout>

Fragment.java

     @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {



    //datasource = new ClassesDataSource(getActivity());
    //datasource.open();


    //Classes value = datasource.getClasses(this.getArguments().getString("id"));

    View v =  inflater.inflate(R.layout.fragment_class, container, false);
    TextView tv = (TextView) v.findViewById(R.id.classdetail);


    tv.setText("test_IDE");
    tv.setTextColor(Color.BLUE);



      // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_class, container, false);

}

Your problem is that you're inflating the view twice.
Replace:

return inflater.inflate(R.layout.fragment_class, container, false);

with:

return v;

To explain what the problem is, you are inflating the view, setting the text and text color, and then inflating a different view and assigning that one as the one that will be used for the fragment.

Try using

View v =  getActivity().getLayoutInflater().inflate(R.layout.fragment_class, container, false);

Instead of

View v =  inflater.inflate(R.layout.fragment_class, container, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM