1

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.

Yurets
  • 3,999
  • 17
  • 54
  • 74
dedalo7
  • 11
  • 1
  • 3
  • I tried that guide, but doesn't work, i need to take the data without doInBackground or call startActivity – dedalo7 Jun 15 '15 at 16:58

2 Answers2

0

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();
  • In my case, the data should be send directly from the activity into the fragment, not the opposite – dedalo7 Jun 15 '15 at 17:10
  • That's exactly what this code does @dedalo7. You're passing some data FROM the activity TO the fragment using the fragment's `newInstance` method. Unless your're talking about sending your activity some values AFTER it was created. Is that the case? – Thyen Hong Guedes Chang Jun 15 '15 at 17:17
  • Yes after that the activity do this : new Thread(new Runnable() { @Override public void run() { ViewPager pager = (ViewPager) findViewById(R.id.pager); PagerAdapter adapter = new PaniniViewAdapter(getSupportFragmentManager()); pager.setAdapter(adapter); where the adapter contain the fragment that will be set into the activity viewpager that extends FragmentActivity – dedalo7 Jun 15 '15 at 17:35
-1

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

Community
  • 1
  • 1
Thiago Souto
  • 761
  • 5
  • 13