0

I am doing an android project which uses a Tabhost to link to 3 fragments and need some communication between them. Each tab will include another xml file. I linked different element in different sml file to the .java file. But it doesn't seem to work. Here is the code:

In Main.xml

<TabHost>
 <LinearLayout>
  <include 
 android:id="@+id/fragmentreview" 
 layout="@layout/fragentreview"/>
 </LinearLayout>` 

In fragentreview.xml

<LinearLayout>
<TextView
   android:id="@+id/textViewinfragR"
   android:text="Text in new xml reviews" />
</LinearLayout>`

In fragment.class

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragentreview, container, false);
    txtMsgR = (TextView)rootView.findViewById(R.id.textViewinfragR);
    txtMsgR.setText("Main Car integer = ");
    return rootView;
}

When I launch the app textViewinfragR it should show Main Car integer = but it is still showing Text in new xml reviews. Is there a problem in my code?

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
kayu li
  • 1
  • 2

2 Answers2

0

You are setting the text in the wrong place. Try setting it in onActivityCreated

dibble
  • 299
  • 3
  • 3
0
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    txtMsgR = (TextView) view.findViewById(R.id.textViewinfragR);
    txtMsgR.setText("Main Car integer = ");
}

This should work. The onViewCreated method is called after the inflation is completed.

onCreateView -> onViewCreated -> onActivityCreated

Nelson Hoang
  • 413
  • 2
  • 11
  • Hi I edited my answer slightly. Use the view object given to you for onViewCreated – Nelson Hoang Nov 07 '14 at 22:37
  • Also please ensure that you're using TabHost correctly. [link] (http://www.learn-android-easily.com/2013/07/android-tabwidget-example.html) Here is an example of TabHost using activities but using fragments is fine. – Nelson Hoang Nov 07 '14 at 22:43