Hope you're fine & Congrats for your Work ! Scenario : I have 3 fragments files, Pagegauchefragment, PageMilieufragment & PageDroiteFragment managed by 2 files FragmentsSliderActivity and MyPagerAdapter. The first one and the second one are datas fields with EditText, filled by the user. Last one is a ListView where the datas user files are saved. From the listview (last fragment) the user clicks the file with 3 possibilities : 1. Import the file / 2. delete the file / 3. Cancel (I use the action bar process to save the datas). Now the issue : I can't pass my datas to refresh (with setText) to the other fragments. Log : Tag : IIputConnectionWrapper / Text : getSelectedText on inactive InputConnection, setComposingText on inactive InputConnection.
View W =inflater.inflate(R.layout.page_droite_layout,container, false);
this.mListView = (ListView) W.findViewById(R.id.ListView01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, tabRevues);
this.mListView.setAdapter(adapter);
this.mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
View WW =inflater.inflate(R.layout.page_gauche_layout,container, false);
file_name = (EditText) WW.findViewById(R.id.label_LiquidBase);
//int item = position;
item_name = ((TextView)view).getText().toString();
AlertDialog.Builder ad = new AlertDialog.Builder(getActivity());
ad.setTitle("Selection Recettes : " + item_name);
ad.setMessage("Que souhaitez-vous faire ?");
ad.setPositiveButton("Importer",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
try {
Here I pass a text to setText (for example) :
file_name.setText("blahblah");
I think I'm in a Void method but I can't find out how to pass the variables... ANY help whatsoever will be greatly appreciated! Christopher
Passing data directly between Fragments is never recommended. Instead, pass your data from Fragment X up to your FragmentsSliderActivity which will pass this data on to your Fragment Y. You do that by way of an interface defined in your fragment class and instantiate a callback that is defined in onAttach().
More information on how to do this here Communication With other Fragments
Quick example, consider Fragment A and Fragment B. Fragment A is a list fragment and whenever an item is selected it will change what is displayed in Fragment B. Simple enough, right?
At first, define Fragment A as such.
public class FragmentA extends ListFragment{
//onCreateView blah blah blah
}
And here's Fragment B
public class FragmentB extends Fragment{
//onCreateView blah blah blah
}
And here's my FragmentActivity that will govern them both
public class MainActivity extends FragmentActivity{
//onCreate
//set up your fragments
}
Presumably you have something like this already, now here's how you would change FragmentA(the list fragment that we need to get some data from).
public class FragmentA extends ListFragment implements onListItemSelectedListener, onItemClickListener{
OnListItemSelectedListener mListener;
//onCreateView blah blah blah
// Container Activity must implement this interface
public interface OnListItemSelectedListener {
public void onListItemSelected(int position);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mListener = (OnListItemSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnListItemSelectedListener");
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
//Here's where you would get the position of the item selected in the list, and pass that position(and whatever other data you need to) up to the activity
//by way of the interface you defined in onAttach
mListener.onListItemSelected(position);
}
The most important consideration here is that your parent Activity implements this interface, or else you will get an exception. If implemented successfully, everytime an item in your list fragment is selected, your Activity will be notified of it's position. Obviously you could alter your interface with any number or type of parameters, in this example we're just passing in our integer position. Hope this clarifies a bit man, good luck.