Android xml布局项目不更新

问题描述:

我有一个细节片段,我有几个项目,主要是TextView。我也有一个CursorLoader,onLoadFinished应该在布局中设置所有这些值。Android xml布局项目不更新

例如,在这个片段中,它打印出的文本被设置为我从ContentProvider的

收到
String dur = this.expected_duration + " " + this.durationUnit; 
    Log.v(LOG_TAG, "SETTING DURATION " + dur); 
    if(dur != null || !"".equals(dur)) { 
     if(durationTextView != null) { 
      Log.v(LOG_TAG, "SETTING TEXT VIEW " + dur); 
      durationTextView.setText(dur); 
     } 
    }else{ 
     Log.v(LOG_TAG, "FAIL SETTING TEXT VIEW " + dur); 
    } 

enter image description here

我在日志中看到结果的价值,但这样的结果是永远得到实际的TextView。

我知道一个事实,它不是一个知名度或文字字体的问题。我将TextView突出显示为粉红色,并确保模拟文本显示在屏幕上。我有一种感觉,我可能会创建一个重叠的片段(不知道它是否可能)。我可以很好地实例化所有TextView并提取所有值,但不能将它们放在屏幕上。请帮忙。另外,如果我的重复片段理论是正确的,请解释你如何追踪它。谢谢。

我的布局文件(以防万一)

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.jennyeckstein.udacitycoursepicker.DetailActivityFragment" 
    > 



    <ScrollView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/scrollView" > 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 


      <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/course_subtitle" 
        tools:text="How to Make an Android App"/> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:id="@+id/course_layout_detail" 
        android:layout_below="@id/course_subtitle" 
        > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:id="@+id/duration" 
        android:background="@color/pink" 
        android:textSize="16sp" 
        tools:text="333 years"/> 
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:id="@+id/course_level" 
        tools:text="ADVANCED"/> 
      </LinearLayout> 

      <Button 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/begin_course_button" 
       android:text="Go to Udacity" 
       android:layout_below="@+id/course_layout_detail"/> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/required_knowledge" 
       android:layout_below="@id/begin_course_button" 
       tools:text="If you are new to programming and don’t know where to start" 
       /> 



      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@+id/required_knowledge" 
       android:id="@+id/summary"/> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@+id/summary" 
       android:background="@color/green" 
       android:id="@+id/syllabus"/> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@+id/syllabus" 
       android:id="@+id/faq"/> 



    </RelativeLayout> 
    </ScrollView> 
</FrameLayout> 

源代码上GitHub Repo

布局名称:fragment_detail.xml和 片段名称:DetailActivityFragment.java

的问题是,因为您正在加载的细节您可能会发生多次片段化。

在你的布局文件activity_detail.xml,你对线79的静态信息片段 - 84:

 <fragment 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:name="com.jennyeckstein.udacitycoursepicker.DetailActivityFragment" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      /> 

当你的应用在DetailActivity.java到达方法onCreate 42行调用setContentView(R.layout.activity_detail);它加载你的第一个细节片段。

然后在相同的方法行47-49,你叫

DetailActivityFragment fragment = new DetailActivityFragment(); 
    fragment.setArguments(arguments); 
    getFragmentManager().beginTransaction().add(R.id.fragment_detail_container, fragment).commit(); 

它加载的细节片段的第二个副本。

activity_detail.xml,试图注释掉线线79 - 84,

 <fragment 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:name="com.jennyeckstein.udacitycoursepicker.DetailActivityFragment" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      /> 

如果你现在运行你的应用程序,并选择一门课程,详细信息屏幕将在您的文本视图以文本显示打开与粉红色的背景如预期的。

有关静态片段和动态片段的更多信息,您可能会发现以下Android文档很有帮助。 https://developer.android.com/guide/components/fragments.html

+0

@John_Shea,thanx这么多,我完全错过了它,你的解决方案完全工作。我正在通过代码和它的布局,失败了我!做得好! – Jenny