Send data from activity to viewpager fragment in Android

7.9k views Asked by At

I have a ViewPager that needs to get int values from a previous Activity that contains two EditTexts which their values are put into two int variables. How I can take these values and send into the Fragment? I tried bundle and intent, but doesn't work.

Here is the code: Activity One:

    final EditText editText2 = (EditText) findViewById(R.id.posti); //EDIT TEXT BOX MENU POSTI
    final EditText editText = (EditText) findViewById(R.id.dialogText); // EDIT TEXT BOX POSTI ORARIO
    final FrameLayout editTextLayout = (FrameLayout) findViewById(R.id.frame_ordinazioni); //EDIT TEXT MENU BOX
    final Button button_conferma = (Button) findViewById(R.id.bottone_conferma); //BOTTONE MENU BOX
    editTextLayout.setVisibility(View.VISIBLE);


    button_conferma.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
             final int numero_tavolo = Integer.parseInt(editText.getText().toString());
             final int posti= Integer.parseInt(editText2.getText().toString());
             Calendar calendar = Calendar.getInstance();
             int hour = calendar.get(Calendar.HOUR);
             int minute = calendar.get(Calendar.MINUTE);

             //guide that I follow  
             final Bundle bundle = new Bundle();
             bundle.putInt("Tavoli",numero_tavolo);
             bundle.putInt("Posti",posti);
             PaniniMenuFragment fragobj = new PaniniMenuFragment();
             fragobj.setArguments(bundle);
         }
     });

Fragment on ViewPager:

public View onCreateView(final LayoutInflater inflater,ViewGroup      
        container,Bundle savedInstanceState){

     View view=(View)inflater.inflate(R.layout.fragment_menupanini, container,false);
     ImageView iv = (ImageView) view.findViewById(R.id.panini_images);
     TextView tv=(TextView)view.findViewById(R.id.panini_ingredienti);
     TextView names=(TextView)view.findViewById(R.id.panini_names);
     Button ordina=(Button)view.findViewById(R.id.panini_ordina);
     TextView prezzo=(TextView)view.findViewById(R.id.panini_prezzo);
     final TavoloTable myDB=new TavoloTable(getActivity().getApplicationContext());


     int imageId = getArguments().getInt(IMAGE_ID);
     final String panini_prezzo=getArguments().getString(STRING_PREZZO);
     int position = getArguments().getInt(POSITION);
     String StringIngredients=getArguments().getString(STRING_INGREDIENTS);
     final String StringName=getArguments().getString(STRING_NAME);

     iv.setImageResource(imageId);
     tv.setText(StringIngredients);
     names.setText(StringName);
     prezzo.setText(panini_prezzo);


     ordina.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             myDB.open();

             Bundle bundle=new Bundle();
             int numero_tavolo=bundle.getInt("Tavoli");;
             int numero_posti= bundle.getInt("Posti");

             counter++;
         }
     });
}

Sorry for my bad english.

2

There are 2 answers

1
Thiago Souto On

You can send the data From Activity ONE to Activity TWO using bundle via intent and set the arguments of this fragment.

@ρяσѕρєя K answered how to send data from activity to a fragment here-> Send data from activity to fragment in android

Edit

You can use a Delegation pattern!

In software engineering, the delegation pattern is a design pattern in object-oriented programming where an object, instead of performing one of its stated tasks, delegates that task to an associated helper object. There is an Inversion of Responsibility in which a helper object, known as a delegate, is given the responsibility to execute a task for the delegator. The delegation pattern is one of the fundamental abstraction patterns that underlie other software patterns such as composition (also referred to as aggregation), mixins and aspects." - Wikipedia.

You can return a object with these values.

Example: https://en.wikipedia.org/wiki/Delegation_pattern

3
Thyen Hong Guedes Chang On

Have you tried to pass those values via parameters on a static constructor method?

Here's an example:

public class YourFragment extends Fragment{

    private int numberOne, numberTwo;

    public static YourFragment(int numberOne, int numberTwo) newInstance{
        YourFragment fragment = new YourFragment();
        Bundle args = new Bundle();
        args.putInt("numberOne", numberOne);
        args.putInt("numberTwo", numberTwo);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if(args != null){
            this.numberOne = args.getInt("numberOne");
            this.numberTwo = args.getInt("numberTwo");
        }
    }

}

And then you just need to create your fragment on your activity and call it, like so:

YourFragment fragment = YourFragment.newInstance(numberOne, numberTwo);
getSupportFragmentManager().
    beginTransaction().
    replace(R.id.containerId, fragment).
    commit();