I have a ViewPager
that needs to get int
values from a previous Activity
that contains two EditText
s 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.
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!
You can return a object with these values.
Example: https://en.wikipedia.org/wiki/Delegation_pattern