Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textviews in a Fragment layout don't change when calling SetText()

My application (min level 13) is an Activity that uses tabs in the action bar to manage a couple fragments, very similar to this.

Now, the activity starts a service which does continuous computation and returns values which I would like to display in the Fragments. The Activity - Service communication is implemented through broadcast receivers and the Activity shuffles the data off to the appropriate Fragment.

Everything seems setup correctly and the data makes it to the Fragment update method but when I try to display the new values in textviews, the new values are never displayed.

The code to change the textviews:

    TextView tv = (TextView) getView().findViewById(R.id.fieldNavGpsTime);
    Double doub = input.getDoubleExtra("com.some.thing.GPS_TIME", -1.0);
    tv.setText(doub.toString());

The code to call the Fragments update methods from the broadcast receiver in the Activity:

    NavigationGuiFragment navfrag = (NavigationGuiFragment) getFragmentManager().findFragmentByTag("navigation");
    if (navfrag != null && navfrag.isResumed())
        navfrag.UpdateNavUI(intent);

I've noticed that isVisible() doesn't seem to ever return true, but I'm not sure what it means or how to change it.

Additionally, I can't seem to add an imageview to a Fragment programmatically. Here's the code (which resides in onActivityCreated()):

    this.compass = new BasicCompass(getActivity());
    LinearLayout ll = (LinearLayout) getView().findViewById(R.id.nav_hrztl_lnly);
    ll.addView(this.compass);

The BasicCompass constructor takes a Context, admittedly I'm not completely sure what I'm passing in is correct.

The code for this was more or less taken from a working Activity and dropped into a Fragment to allow for tabs. I'm open to suggestion in regards to changing the structure of the code.

Thanks for any help.

EDIT

The xml layout of the Fragment:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/nav_hrztl_lnly"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:baselineAligned="false" >

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="600dp"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:focusable="true"
    android:focusableInTouchMode="true" >

    <EditText
        android:id="@+id/labelNavGpsTime"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/gps_time" />

    <EditText
        android:id="@+id/fieldNavGpsTime"
        style="@style/field_padding"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/zero_3_digits"
        android:inputType="numberDecimal" />

    <EditText
        android:id="@+id/labelNavLatitude"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/latitude" />

    <EditText
        android:id="@+id/fieldNavLatitude"
        style="@style/field_padding"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/zero_6_digits"
        android:inputType="numberDecimal" />

    <EditText
        android:id="@+id/labelNavLongitude"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/longitude" />

    <EditText
        android:id="@+id/fieldNavLongitude"
        style="@style/field_padding"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/zero_6_digits"
        android:inputType="numberDecimal" />

    <EditText
        android:id="@+id/labelNavAltitude"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/altitude" />

    <EditText
        android:id="@+id/fieldNavAltitude"
        style="@style/field_padding"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/zero_3_digits"
        android:inputType="numberDecimal" />

    <EditText
        android:id="@+id/labelNavRoll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/roll" />

    <EditText
        android:id="@+id/fieldNavRoll"
        style="@style/field_padding"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/zero_6_digits"
        android:inputType="numberDecimal" />

    <EditText
        android:id="@+id/labelNavPitch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/pitch" />

    <EditText
        android:id="@+id/fieldNavPitch"
        style="@style/field_padding"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/zero_6_digits"
        android:inputType="numberDecimal" />

    <EditText
        android:id="@+id/labelNavAzimuth"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/azimuth_heading" />

    <EditText
        android:id="@+id/fieldNavAzimuth"
        style="@style/field_padding"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/zero_6_digits"
        android:inputType="numberDecimal" />

    <LinearLayout
        android:id="@+id/nav_rdbtn_lnly"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/rdbtnNavGpsAvailability"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/gps_avail" />

        <RadioButton
            android:id="@+id/rdbtnNavZuptStatus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/zupt_stat" />

    </LinearLayout>

</LinearLayout>

And the Fragment that uses it:

public class NavigationGuiFragment extends Fragment
{
private RadioButton gpsRdBtn;
private RadioButton zuptRdBtn;
private BasicCompass compass;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    View fragview =  inflater.inflate(R.layout.navigation_fragment, container, false);

    // sets up the rose image that serves as a compass in the GUI
    this.compass = new BasicCompass(getActivity());
    LinearLayout ll = (LinearLayout) fragview.findViewById(R.id.nav_hrztl_lnly);
    ll.addView(this.compass);

    return fragview;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    super.onActivityCreated(savedInstanceState);
    getActivity().setContentView(R.layout.navigation_fragment);

    //Initialize the radio buttons
    gpsRdBtn = (RadioButton) getView().findViewById(R.id.rdbtnNavGpsAvailability);
    gpsRdBtn.setChecked(false);

    zuptRdBtn = (RadioButton) getView().findViewById(R.id.rdbtnNavZuptStatus);
    zuptRdBtn.setChecked(false);    
}

@Override
public void onResume() 
{
    super.onResume();

    if (!IsMyServiceRunning())
    {
        gpsRdBtn.setChecked(false);
        zuptRdBtn.setChecked(false);
    }
}

public void UpdateNavUI(Intent input)
{       
    TextView tv = (TextView) getView().findViewById(R.id.fieldNavGpsTime);
    Double doub = input.getDoubleExtra("com.some.thing.GPS_TIME", -1.0);
    tv.setText(doub.toString());

    tv = (TextView) getView().findViewById(R.id.fieldNavLatitude);
    doub = input.getDoubleExtra("com.some.thing.LATITUDE", 100000.0);
    tv.setText(doub.toString());

    tv = (TextView) getView().findViewById(R.id.fieldNavLongitude);
    doub = input.getDoubleExtra("com.some.thing.LONGITUDE", 100000.0);
    tv.setText(doub.toString());

    tv = (TextView) getView().findViewById(R.id.fieldNavAltitude);
    doub = input.getDoubleExtra("com.some.thing.ALTITUDE", -1.0);
    tv.setText(doub.toString());

    tv = (TextView) getView().findViewById(R.id.fieldNavRoll);
    doub = input.getDoubleExtra("com.some.androidndk.ROLL", 361.0);
    tv.setText(doub.toString());

    tv = (TextView) getView().findViewById(R.id.fieldNavPitch);
    doub = input.getDoubleExtra("com.some.thing.PITCH", 361.0);
    tv.setText(doub.toString());

    tv = (TextView) getView().findViewById(R.id.fieldNavAzimuth);
    doub = input.getDoubleExtra("com.some.thing.AZIMUTH", 361.0);
    tv.setText(doub.toString());

    this.compass.SetDirection(doub.floatValue());

    boolean bool = input.getBooleanExtra("com.some.thing.ZUPT_STATUS", false);
    zuptRdBtn.setChecked(bool);

    UpdateGpsIndicator(input);
}

public void UpdateGpsIndicator(Intent input)
{
    boolean bool = input.getBooleanExtra("com.some.thing.GPS_ON", false);
    gpsRdBtn.setChecked(bool);
}

private boolean IsMyServiceRunning()
{
    ActivityManager manager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
    {
        if ("com.some.thing.Service".equals(service.service.getClassName())) 
            return true;
    }

    return false;
}

}

like image 246
Matt Avatar asked Feb 02 '12 20:02

Matt


1 Answers

this line:

getActivity().setContentView(R.layout.navigation_fragment);

should be called in Activity.onCreate() and make sure it is just called once. In your code it will be called every time Fragment moves to active state. And the TextView and RaidoButton stuff will be reset to state define in the layout xml.

Checkout Fragment lifecycle here.

UPDATE: Some view widget's state will be kept by Activity, e.g TextView. Try move your setXXX() method to onResume(). I have experience that setXXX() is not working in onActivityCreated() but works well in onResume().

like image 103
Hank Avatar answered Nov 10 '22 23:11

Hank