简体   繁体   中英

Update textview inside fragment from MainActivity

this is my first stackoverflow post.
I was trying to build an app which retrive sensor data with LocalBroadcastManager and then update TextView in already attached fragment inside container.
I tried to call method homeFragment.passData() from MainActivity but didnt succeed.
My guess is because the fragment already inflated so it cant be updated by calling that method.

Here is MainActivity code where I call method to update the textview

@Override
    public void onReceive(Context context, Intent intent) {
        String azimuthValue = intent.getStringExtra("azimuth");
        String pitchValue = intent.getStringExtra("pitch");
        String rollValue = intent.getStringExtra("roll");


        homeFragment.passData(azimuthValue, pitchValue, rollValue);
    }


And here is code for HomeFragment

public class HomeFragment extends Fragment {

private static final String TAG = "HomeFragment";

private Context mContext;

private TextView xValueTextView;
private TextView yValueTextView;
private TextView zValueTextView;

private OnFragmentInteractionListener mListener;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

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

    xValueTextView = (TextView) rootView.findViewById(R.id.xValueTextView);
    yValueTextView = (TextView) rootView.findViewById(R.id.yValueTextView);
    zValueTextView = (TextView) rootView.findViewById(R.id.zValueTextView);

    Log.d(TAG, "onCreateView: layout inflated");

    return rootView;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    mContext = context;
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {}

public void passData(String x, String y, String z) {
    xValueTextView.setText(x);
    yValueTextView.setText(y);
    zValueTextView.setText(z);

    Log.i(TAG, "updateTextView: TextView value: " + xValueTextView.getText().toString() + "||" + yValueTextView.getText().toString() + "||" + zValueTextView.getText().toString() );
}

}


Although logcat textview.getText().toString show updated value, the actual view is not yet updated

10-21 14:03:56.240 19338-19338/pro.adhi.willyam.orientation I/HomeFragment: updateTextView: TextView value: 45||-34||4

here is screenshot from my phone : https://i.stack.imgur.com/ZDsad.png

So how to properly update textview inside fragment like what I'm trying to achieve?
I hope my question is understandable. Thankyou

You need to set up setter/getter methods in your fragment.

public class HomeFragment extends Fragment {

TextView tv;

    public void setTextView(String text)
    {
         TextView tv = (TextView) findViewById( *your id here* );
         tv.setText(text);
    }
}

In the MainActivity you just use this call:

    public class MainActivity extends AppCompatActivity {

    ...

    HomeFragment.setTextView("Hello World!");

    ...

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM