2016-05-30 101 views
0

我有一個細節片段,我有幾個項目,主要是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

回答

1

的問題是,因爲您正在加載的細節您可能會發生多次片段化。

在你的佈局文件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