I have a problem,I want to send one argument extraData
from my activityVenteActivity
to one of his fragmentPageAdjuFragment
at the creation of the activity.
I have 3 fragments on this activity for slide between them, for the moment I work only on PageAdjuFragment
.
I see on stackoverflow how to use newinstantiate()
for passing a bundle in argument for a new instance of PageAdjuFragment
but that don't work for me :(.
You can see here my activity:
public class VenteActivity extends FragmentActivity {
private PagerAdapter mPagerAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.viewpager2);
creationPage();
//Recovery
Intent sender=getIntent();
String extraData=sender.getExtras().getString("message");
Log.v("nivyolo",extraData);//this is the argument which i would send
//Sending
PageAdjuFragment.newInstance(extraData); // the problem start here
}
public void creationPage() {
// Creation of the list
List<Fragment> fragments = new Vector<Fragment>();
// Add Fragments in a list
fragments.add(Fragment.instantiate(this,PageOffreFragment.class.getName()));
fragments.add(Fragment.instantiate(this,PageAdjuFragment.class.getName()));
fragments.add(Fragment.instantiate(this,PageNewPartFragment.class.getName()));
// Creation of theadapter
this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager2);
// Affectation on the ViewPager
pager.setAdapter(this.mPagerAdapter);
}
}
My fragment that I want to pass an argument to the creation
public class PageAdjuFragment extends Fragment {
public static PageAdjuFragment newInstance(String vente) {
PageAdjuFragment myFragment = new PageAdjuFragment();
Bundle args = new Bundle();
args.putString("vente", vente);
myFragment.setArguments(args);
return myFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
final View v=inflater.inflate(R.layout.page_adju_layout, container, false);
final Button b=(Button)v.findViewById(R.id.btconf);
final RadioGroup radiopmt = (RadioGroup) v.findViewById(R.id.radiopmt);
final RadioButton rb1 = (RadioButton)v.findViewById(R.id.rb1);
final RadioButton rb2 = (RadioButton)v.findViewById(R.id.rb2);
final AutoCompleteTextView tv1 = (AutoCompleteTextView)v.findViewById(R.id.actv1);
final AutoCompleteTextView tv2 = (AutoCompleteTextView)v.findViewById(R.id.actv2);
final EditText tv3 = (EditText)v.findViewById(R.id.edt);
////START PROBLEM////////
String vente;
// Recovery of the parameter
Log.v("adju","before recup");
Bundle arg = getArguments(); // the problem
Log.v("adju","after recup");
if (arg == null) {// all the time arg is null
Log.v("args","arguments is null ");
vente = "yolo";
} else {
Log.v("args2","arguments not null ");
vente= arg.getString("vente");
Log.v("nivyolobonnepage",vente);
}
tv3.setText(vente); //just a test
////END PROBLEM////////
//Data adaptation on TextAuto
List<String> OBJET = null;
List<String> PARTI = null;
try {
OBJET = InitObjet();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
PARTI = InitPart();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_dropdown_item_1line, OBJET);
tv1.setAdapter(adapter);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_dropdown_item_1line, PARTI);
tv2.setAdapter(adapter2);
Log.v("papaouté","ici"); //Listerner Boutton Confirmer
b.setOnClickListener(new OnClickListener() {...});
//Listener on the widget value's changes
tv1.addTextChangedListener(new TextWatcher() {...});
tv2.addTextChangedListener(new TextWatcher() {...});
tv3.addTextChangedListener(new TextWatcher() {...});
radiopmt.setOnCheckedChangeListener(new OnCheckedChangeListener() {...});
return v;
}
//Create the List of object
public List<String> InitObjet() throws IOException{...}
//Create the List of participant
public List<String> InitPart() throws IOException {...}
//Write in the CSV FILE "achat"
public void ecrire() throws IOException{...}
}
And finally the Page Adapter
public class MyPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragments;
//On fournit à l'adapter la liste des fragments à afficher
public MyPagerAdapter(FragmentManager fm, List fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int pos) {
return this.fragments.get(pos);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
and the wiewpager2.xml
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/viewpager2">
</android.support.v4.view.ViewPager>
the logCat
05-14 10:13:51.919: V/nivyolo(10005): vente1
05-14 10:13:52.169: V/adju(10005): before recup
05-14 10:13:52.169: V/adju(10005): after recup
05-14 10:13:52.169: V/args(10005): arguments is null
05-14 10:13:52.209: V/papaouté(10005): ici
The argument arg
is always null