简体   繁体   English

从MainActivity更新片段内的textview

[英]Update textview inside fragment from MainActivity

this is my first stackoverflow post. 这是我的第一个stackoverflow帖子。
I was trying to build an app which retrive sensor data with LocalBroadcastManager and then update TextView in already attached fragment inside container. 我正在尝试构建一个应用程序,该应用程序使用LocalBroadcastManager检索传感器数据,然后在容器内已连接的片段中更新TextView。
I tried to call method homeFragment.passData() from MainActivity but didnt succeed. 我尝试从MainActivity调用方法homeFragment.passData(),但未成功。
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 这是MainActivity代码,我在其中调用方法来更新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 这是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 尽管logcat textview.getText()。toString显示更新的值,但实际视图尚未更新

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 这是我手机的屏幕截图: https : //i.stack.imgur.com/ZDsad.png

So how to properly update textview inside fragment like what I'm trying to achieve? 那么,如何像我想要实现的那样正确更新片段内部的textview?
I hope my question is understandable. 我希望我的问题是可以理解的。 Thankyou 谢谢

You need to set up setter/getter methods in your fragment. 您需要在片段中设置setter / getter方法。

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: 在MainActivity中,您只需使用此调用:

    public class MainActivity extends AppCompatActivity {

    ...

    HomeFragment.setTextView("Hello World!");

    ...

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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