简体   繁体   English

TextView没有更新

[英]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. 我只是想在Fragment中为TextView设置一个值。 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. 尽管显示了预定义的文本“ test”,所以我可以排除任何与布局有关的问题。 Any ideas on this? 有什么想法吗? Thanks !! 谢谢 !!

fragment_class.xml 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 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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