2017-12-03 14 views
0

3つのフラグメントでタブ付きアクティビティを作成しました。 MainActivity Javaファイルからフラグメント内のTextViewのテキストを更新するにはどうすればよいですか?ViewPager内のTextViewをanohterフラグメントから更新する

TabStatus.java:私はTextViewにtbStateを更新したい

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/constraintLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.sanderjochems.bluetoothdata.MainActivity" 
    tools:layout_editor_absoluteY="81dp"> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="16dp" 
     android:layout_marginStart="16dp" 
     android:layout_marginTop="16dp" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toTopOf="parent"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:paddingBottom="16dp" 
      android:paddingLeft="16dp" 
      android:paddingRight="16dp" 
      android:paddingTop="24dp"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/title_bluetooth" 
       android:textAllCaps="false" 
       android:textColor="@color/colorPrimaryText" 
       android:textSize="24sp" 
       android:textStyle="bold" /> 

      <Space 
       android:layout_width="match_parent" 
       android:layout_height="8dp" /> 

      <TextView 
       android:id="@+id/tbMacAddress" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/text_mac_address" /> 

      <TextView 
       android:id="@+id/tbState" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/text_state" /> 

     </LinearLayout> 

    </android.support.v7.widget.CardView> 

</android.support.constraint.ConstraintLayout> 

package com.sanderjochems.bluetoothdata; 

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class TabStatus extends Fragment { 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_status, container, false); 
    } 
} 

fragment_status.xml。どうやってやるの?

私はLayoutInflaterを使用しようとしましたが、それは動作しませんでした。

私はそのためのコードのこの部分を使用:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View layoutStatus = inflater.inflate(R.layout.fragment_status, null); 

tvCurrentState = (TextView) layoutStatus.findViewById(R.id.tbState); 

よろしく、 サンダーJochems

答えて

関連する問題