2017-08-13 89 views
0

我想從我的mainActivity中調用一個函數來更改我的一個片段中的TextView。我已經閱讀了幾篇文章,介紹了做這件事的最佳方式,但由於某種原因,他們都沒有爲我工作。我知道這個功能正在工作,因爲一旦按下按鈕,烤麪包就會出現,但由於某種原因,文字不會改變。我想知道這個問題可能是什麼,或者如果我只是錯過了一個額外的步驟。無法在片段中設置文本

這裏被稱爲我的mainActivity

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

兩者都出現了,我添加了第二個敬酒,以查看是否能夠從該活動調用我的updatInfo函數。它正在工作,但由於某種原因,它不會將文本設置爲「測試」。 – l34lo1

回答

0

要在活動課的片段做出方法的調用,你需要像代碼這個:

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問題是你總是在你的活動中創建一個新的'Fragment',使用:'Tab1Fragment tab1Fragment = new Tab1Fragment();'。你也應該有查找片段的情況,因爲它是由android框架管理的。看到分支使用:'getSupportFragmentManager()。findFragmentById(R.layout.tab1_fragment);'就像我在我的答案中提到的。你需要這兩種情況。 – dan

0

當您添加的片段標籤設置成這樣的:

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

雖然更新的TextView使用findFragmentByTag()

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

謝謝,但它仍然不適合我。看來我的問題是與textview不更新。 – l34lo1

+0

是否嘗試調試代碼?在調試器模式下運行你的應用程序,並檢查調試器是否進入updateInfo()方法 – Harsh