2017-08-13 14 views
0

私のmainActivityから関数を呼び出して、私のフラグメントの1つでTextViewを変更しようとしています。私はそれをする最善の方法でいくつかの記事を読んだが、何らかの理由で、それらのどれも私のために働くようだ。私は関数が機能していることを知っている。なぜなら、一旦プレスを押すとトーストが始まるが、なんらかの理由でテキストは変化しないからである。私はその問題が何であるか、あるいは私がもう一つのステップを欠いているのだろうかと思っていました。フラグメント内にテキストを設定できません

は、ここでの方法は、ここで

public class MainActivity extends AppCompatActivity implements Tab1Fragment.OnCalcClickListener{ 
private static final String TAG = "MainActivity"; 
private SectionsPageAdapter mSectionsPageAdpater; 
private ViewPager mViewPager; 
Tab1Fragment tab1Fragment = new Tab1Fragment(); 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Log.d(TAG, "onCreate: Starting"); 

    //initializing FragmentManager so the fragments can communicate 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.add(R.id.container, tab1Fragment); 
    fragmentTransaction.commit(); 

    //declare sections page adapter 
    mSectionsPageAdpater = new SectionsPageAdapter(getSupportFragmentManager()); 

    //Set up the view pager with the sections adapter 
    mViewPager = (ViewPager) findViewById(R.id.container); 
    setUpViewPager(mViewPager); 

    //create a tab layout object and set it's id to tabs (mainActivity.xml) 
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(mViewPager); 
    // 
} 

// create a sections page view adapter 
private void setUpViewPager(ViewPager viewPager){ 
    SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager()); 
    adapter.addFragment(new Tab1Fragment(), "Day"); 
    adapter.addFragment(new Tab2Fragment(), "Info"); 
    adapter.addFragment(new Tab3Fragment(), "Week"); 
    viewPager.setAdapter(adapter); 
} 

//initialise calculator object 
Calculator mainCalculator = new Calculator(); 


@Override 
public void calculateClick(int to_calculate) { 
    switch (to_calculate){ 
     case 1: 
      Toast.makeText(getBaseContext(),"working", Toast.LENGTH_SHORT).show(); 
      mainCalculator.freqDay = mainCalculator.freqDay + 1; 
      mainCalculator.freqWeek = mainCalculator.freqWeek + 1; 
      mainCalculator.getTotalDay(); 
      tab1Fragment.updateInfo(); 
      break; 
    } 
} 

は、あなたのようなものを必要とする活動のクラスであなたのフラグメントからメソッドへの呼び出しを行うために、私のフラグメントのコード

public class Tab1Fragment extends Fragment implements 
View.OnClickListener{ 
private static final String TAG = "Tab1Fragment"; 
//Establishing the buttons & Methods 
Button btn1; 
Button btn2; 
TextView dayView; 
OnCalcClickListener onCalcClickListener; 

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


    //Connecting the buttons to the xml 
    btn1 = (Button) view.findViewById(R.id.btn_1); 
    btn1.setOnClickListener(this); 
    btn2 = (Button) view.findViewById(R.id.btn_2); 
    btn2.setOnClickListener(this); 
    dayView = (TextView) view.findViewById(R.id.total_Sales_Day); 
    return view; 
} 

//Onclick listener for buttons 
public void setOnClickListener(View.OnClickListener listener) { 
    btn1.setOnClickListener(listener); 
    btn2.setOnClickListener(listener); 
} 


//method that will bring data back from activity and set the text 
public void updateInfo(){ 
    Toast.makeText(getContext(),"65", Toast.LENGTH_SHORT).show(); 
    dayView.setText("test"); 

} 
+0

を使用してフラグメントのインスタンスを取得し、どのように 'tab1Fragment'を初期化したのですか? –

+0

別の質問:どのトーストが出現するのですか? "働く"または "65"! –

+0

どちらも表示され、2番目のトーストを追加して、アクティビティからupdatInfo関数を呼び出せるかどうかを確認しました。何らかの理由で動作していますが、テキストを「テスト」に設定しないだけです。 – l34lo1

答えて

0

である私のmainActivityに呼び出されていますこれは:

public static class MainActivity extends Activity 
     implements HeadlinesFragment.OnHeadlineSelectedListener{ 
    ... 

    public void onArticleSelected(int position) { 
     // The user selected the headline of an article from the HeadlinesFragment 
     // Do something here to display that article 

     ArticleFragment articleFrag = (ArticleFragment) 
       getSupportFragmentManager().findFragmentById(R.id.article_fragment); 

     if (articleFrag != null) { 
      // If article frag is available, we're in two-pane layout... 

      // Call a method in the ArticleFragment to update its content 
      articleFrag.updateArticleView(position); 
     } else { 
      // Otherwise, we're in the one-pane layout and must swap frags... 

      // Create fragment and give it an argument for the selected article 
      ArticleFragment newFragment = new ArticleFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ArticleFragment.ARG_POSITION, position); 
      newFragment.setArguments(args); 

      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 

      // Replace whatever is in the fragment_container view with this fragment, 
      // and add the transaction to the back stack so the user can navigate back 
      transaction.replace(R.id.fragment_container, newFragment); 
      transaction.addToBackStack(null); 

      // Commit the transaction 
      transaction.commit(); 
     } 
    } 
} 

アクティビティはupdateArticleViewメソッドの代わりに、updateInfoメソッドを使用しますが、そのアイデアを得ることができます。

one-paneシナリオでは、内容を交換して、Bundleオブジェクトを使用して引数をプッシュする必要があります。

詳細はDeliver a Message to a Fragmentを参照してください。あなたはこのようにそれにタグを設定するフラグメント追加

+0

私の悪い、私は私の主な活動の残りの部分を投稿していない。私の断片は主な活動から文字列を受け取ることを管理していますが、何らかの理由でtextViewが更新されていません。これがonCreate関数によって引き起こされるかどうか知っていますか? – l34lo1

+0

@ l34lo1問題は、あなたの活動の中で、常に 'Tab1Fragment tab1Fragment = new Tab1Fragment();'を使って新しい 'Fragment'を作成しているということです。 Androidフレームワークによって管理されているため、フラグメントの参照も必要です。私の答えで述べたように、 'getSupportFragmentManager()。findFragmentById(R.layout.tab1_fragment);'を使ってブランチを参照してください。あなたは両方の場合が必要です。 – dan

0

:のTextViewを更新中

MyFragment frag = new MyFragment(); 
frag.setArguments(getIntent().getExtras()); 
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, frag, "TAG").commit(); 

findFragmentByTag()

MyFragment fragment = (MyFragment) getSupportFragmentManager().findFragmentByTag("TAG"); 
fragment.updateInfo(); 
+0

ありがとうございますが、まだ私のために働いていません。私の問題は、更新されていないtextviewです。 – l34lo1

+0

コードをデバッグしようとしましたか?デバッガモードでアプリケーションを実行し、デバッガがupdateInfo()メソッド内にあるかどうかを確認します。 – Harsh

関連する問題