Parameter passing from one fragment to another

1

I do not know how to work with two fragments, I looked for a few things here but could not find it right.

From Activity to another I use Bundle and it works, but I'm not sure how to use it with Fragment.

I'm working with Navigator Drawer that use fragments. one of the fragments I have a ListView and in this ListView I need to call another fragment with ListView as well.

Ex. first Fragment / ListView (Years) - 2016 - 2015 - 2014 - ....

Follow the code I'm using in the first fragment.

lv_Anos.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // pegas os valores do Text no linha //
        TextView txAno = (TextView) view.findViewById(R.id.tx_Ano);
        // chamar outro fragment e passar paramentro para ele //
        // Variável ->> Param_Ano  


     }
});

The second fragment / ListView - Editions

I need to receive the Selected Year to generate Json of the second fragment.

Variable Param_Ano

new JSONTask().execute("http://www...../edicaoporano.php?Ano=" + Param_Ano);
    
asked by anonymous 04.10.2016 / 01:35

1 answer

3

You should use the bundle too, see an example:

To send to another Fragment

Seu_Fragment fragment = new Seu_Fragment(); 
Bundle bundle = new Bundle();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
bundle.putString("ano", Param_Ano); 
fragment.setArguments(bundle);
fragmentTransaction.replace(viewID, fragment);
fragmentTransaction.commit();

And to receive in the other Fragment

Bundle mBundle = new Bundle();
if(mBundle != null){
   mBundle = getArguments();
   String ano = mBundle.getString("ano"); 
} 

Now in the second Fragment, you have the string year populated with the value that came from the first fragment

    
04.10.2016 / 14:31