如何在片段中将值设置为Textviews,edittext或图表?

时间:2017-05-12 04:18:47

标签: android android-fragments fragmentmanager

在我开始之前,请允许我说我已经检查了所有关于此的链接,但不幸的是,inflater init方法对我不起作用

Fragment.java

 public View onCreateView(LayoutInflater inflater1, ViewGroup container, Bundle savedInstanceState) {
   View view = inflater1.inflate(R.layout.tab, container, false);
    context = getActivity();
    findViews(view);
    setBar();
    setPie();
    return view;//inflater1.inflate(R.layout.tab, container, false);

}

 private void findViews(View view) {
    inflater=LayoutInflater.from(context);
    root=(ViewGroup)view.findViewById(R.id.root);
   piechart= (PieChart)view.findViewById(R.id.pie_chart);
    barChart = (BarChart)view.findViewById(R.id.barchart);
    popupContentView=inflater.inflate(popupdashboard,root,false);

Activity.java

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard2);
    //Initializing viewPager
    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    tabLayout.setSelectedTabIndicatorColor(Color.MAGENTA);
    //Adding the tabs using addTab() method
    tabLayout.addTab(tabLayout.newTab().setText("Dashboard"));
    tabLayout.addTab(tabLayout.newTab().setText("Transaction Details"));
    tabLayout.addTab(tabLayout.newTab().setText("Your Tab Title2"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    viewPager = (ViewPager) findViewById(R.id.simpleViewPager);
    //Creating our pager adapter
    pager adapter = new pager(getSupportFragmentManager(), tabLayout.getTabCount());

    //Adding adapter to pager
    viewPager.setAdapter(adapter);
   /// if (tabLayout.getSelectedTabPosition()==0){
        init = new Tab1();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.linearLayout, init);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.commit();

任何帮助都将深表感谢

2 个答案:

答案 0 :(得分:0)

你可以尝试这个:

  1. 在函数ViewGroup的本地创建onCreate类型的变量,然后对其进行初始化。

    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.nameoflayout, container, false);
    
  2. 然后,您可以使用以下方法设置TextView:

    TextView txtView = (TextView)root.findViewByID(R.id.nameoftextviewid);
    
  3. 返回变量root

  4. 这正是我在项目中所做的。

答案 1 :(得分:0)

请试一试....

  ViewGroup root = (ViewGroup) inflater.inflate(R.layout.nameoflayout, container, false);

        piechart= (PieChart)view.findViewById(R.id.pie_chart);
        barChart = (BarChart)view.findViewById(R.id.barchart);

     return view;

参考链接.. refer