2017-08-13 57 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