Reputation: 545
I have a detail fragment where i have a few items, mostly TextView. I also have a CursorLoader, that onLoadFinished is supposed to set all those values in the layout.
For example in this snippet, it prints out that text is being set to the value that I received from 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);
}
I see the result in the log but this result is never getting to the actual TextView.
I know for a fact that its not a problem of visibility or text font. I have the TextView hightlighted in pink and made sure that the mock text displays on the screen. I have a feeling that I may be creating duplicate fragments that lay one on top of another (not sure if its possible). I can perfectly instantiate all the TextViews and extract all the values but cannot put them together on the screen. Please help. In addition if my theory of duplicate fragments is correct please explain how you tracked it down. Thank you.
My Layout file (just in case)
<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>
Source code on GitHub Repo
Layout name: fragment_detail.xml and Fragment name: DetailActivityFragment.java
Upvotes: 0
Views: 284
Reputation: 26
The problem is because you are loading the details fragment multiple times as you thought might be happening.
In your layout file activity_detail.xml
, you have a static details fragment on lines 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"
/>
When your app reaches line 42 in method onCreate
in DetailActivity.java
it calls setContentView(R.layout.activity_detail);
which loads your first details fragment.
Then on lines 47-49 in the same method, you call
DetailActivityFragment fragment = new DetailActivityFragment();
fragment.setArguments(arguments);
getFragmentManager().beginTransaction().add(R.id.fragment_detail_container, fragment).commit();
which loads a second copy of the details fragment.
In activity_detail.xml
, trying commenting out lines lines 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"
/>
If you now run your app and select a course, the details screen will open with text showing in your text view with the pink background as desired.
For more information on static versus dynamic fragments you might find the following Android documentation helpful. https://developer.android.com/guide/components/fragments.html
Upvotes: 1