0

I have created 10 tabs programmatically and want to change number of buttons that are placed in fragment when a tab is clicked by user.

My 'MainActivity' code:

public class MainActivity extends Activity {
int numberOfButtons=0;
@Override
  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentA fragmentA = new FragmentA();
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.mainLayout,fragmentA,"fragA");
        transaction.commit();
        ActionBar  actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for (int i=0;i<10;i++)
        {
            ActionBar.Tab tab = actionBar.newTab().setText("Tab"+i).setTabListener(new ActionBar.TabListener() {
                @Override
                public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                    numberOfButtons = 10;
                    sendNumber(numberOfButtons);

                }

                @Override
                public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

                }

                @Override
                public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

                }
            });

            actionBar.addTab(tab);
     }
}

This is my fragment code:

public class FragmentA extends Fragment implements View.OnClickListener{

Button btn;
View myView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    int numberOfButtons= getArguments().getInt("someInt",0);
    LinearLayout view =  new LinearLayout(getActivity());
    // Inflate the layout for this fragment
    view.setOrientation(LinearLayout.VERTICAL);
    view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    for (int i = 0;i<numberOfButtons;i++)
    {
        btn = new Button(getActivity());
        view.addView(new Button(getActivity()));
    }
    myView = view;
    return myView;
}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}
@Override
public void onClick(View v) {

}

}

I am calling the numbeOfButtons method inside the MainActivity and want to send number of buttons to Fragment when a tab is clicked. However the application throws error after line fA.numberOfBtns(numberOfButtons); is called. The error is reflect.InvocationTargetException.

How can I change the number of buttons in Fragment when a tab is clicked?

Blast
  • 955
  • 1
  • 17
  • 40

1 Answers1

1

You may want to use newInstance technique to pass parameters during Fragment initialization. Here is a small example taken from here:

public static MyFragment newInstance(int someInt) {
    MyFragment myFragment = new MyFragment();

    Bundle args = new Bundle();
    args.putInt("someInt", someInt);
    myFragment.setArguments(args);

    return myFragment;
}

And later you can access this parameter:

getArguments().getInt("someInt", 0);
Community
  • 1
  • 1
nikis
  • 11,166
  • 2
  • 35
  • 45
  • Thank you for your anwser. newInstance technique transfers parameter to Fragment an calls the `onCreateView` of Fragment. However new buttons are being added over the old buttons. How can I reset the layout of fargment on tab click? – Blast May 23 '14 at 14:02
  • @Blast post full stacktrace – nikis May 23 '14 at 14:12
  • @Blast i don't see stackstrace – nikis May 23 '14 at 14:54
  • I am sorry for late reply. I can't reach stack trace in android studio . I have some issues with it :/ – Blast May 24 '14 at 08:25