I have several activities that call the same PagerAdapter to have a slidable layout in each of the activities that I call it. My intention is to pass a parameter to others that already happened. I added an element of type int to the constructor and the Android Studio does not detect any failure, but at the time of receiving the value has the same value as when I initialized it to 0. What could be wrong?
Activity:
MyFragmentPagerAdapter pagerAdapter;
pager = (ViewPager) this.findViewById(R.id.viewPager);
// Create an adapter with the fragments we show on the ViewPager
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(
getSupportFragmentManager(), 1);
adapter.addFragment(ScreenSlidePageFragment.newInstance(getResources()
.getColor(R.color.backgroundPager), 0, "Saldo a invertir: ", 1));
adapter.addFragment(ScreenSlidePageFragment.newInstance(getResources()
.getColor(R.color.backgroundPager), 1, "Valores a comprar: ", 1));
pager.setAdapter(adapter);
MyFragmentPagerAdapter:
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
// List of fragments which are going to set in the view pager widget
List<Fragment> fragments;
int id_valor;
/**
* Constructor
*
* @param fm interface for interacting with Fragment objects inside of an
* Activity
*/
public MyFragmentPagerAdapter(FragmentManager fm, int id_valor) {
super(fm);
this.fragments = new ArrayList<Fragment>();
this.id_valor = id_valor;
}
/**
* Add a new fragment in the list.
*
* @param fragment a new fragment
*/
public void addFragment(Fragment fragment) {
this.fragments.add(fragment);
}
@Override
public Fragment getItem(int arg0) {
return this.fragments.get(arg0);
}
@Override
public int getCount() {
return this.fragments.size();
}
public int getId_valor() {
return id_valor;
}
public void setId_valor(int id_valor) {
this.id_valor = id_valor;
}
@Override
public CharSequence getPageTitle(int position) {
return "Página " + (position + 1);
}
}
ScreenSlidePageFragment.java:
public class ScreenSlidePageFragment extends Fragment {
/**
* Key to insert the background color into the mapping of a Bundle.
*/
private static final String BACKGROUND_COLOR = "color";
/**
* Key to insert the index page into the mapping of a Bundle.
*/
private static final String INDEX = "index";
/**
* Key to insert the text into the mapping of a Bundle.
*/
private static final String TEXTO = "index";
ViewPager pagerActual = null;
EditText edtxt;
private int color;
private String textoSta;
private int id_valor;
Saldo saldoObj;
OperacionesBaseDatos opera;
Float precioIntroducido;
/**
* Instancia un nuevo fragment con un background color, el indice de la pagina y un texto
*
* @param color background color
* @param index index page
* @param texto texto de tipo de cotizacion
* @return a new page
*/
public static ScreenSlidePageFragment newInstance(int color, int index, String texto, int id_estado) {
// Instantiate a new fragment
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
// Save the parameters
Bundle bundle = new Bundle();
bundle.putInt(BACKGROUND_COLOR, color);
bundle.putInt(INDEX, index);
bundle.putString(TEXTO, texto);
fragment.setArguments(bundle);
fragment.setRetainInstance(true);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Carga los parametros con los que se sobrecargo cuando la precarga inicial del fragment se completa
this.color = (getArguments() != null) ? getArguments().getInt(
BACKGROUND_COLOR) : Color.DKGRAY;
this.textoSta = (getArguments() != null) ? getArguments().getString(TEXTO)
: "";
this.id_valor = (getArguments() != null) ? getArguments().getInt("id_valor") : 0;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
// Establece el texto del TextView con el que se sobrecargo
TextView tvIndex = (TextView) rootView.findViewById(R.id.tvIndex);
tvIndex.setText(textoSta);
// Establece el color de fondo con el que se sobrecargo
rootView.setBackgroundColor(color);
edtxt = (EditText) rootView.findViewById(R.id.valores);
//Funcion de escucha del Button
Button btnTick = (Button) rootView.findViewById(R.id.buttonInvertirTick);
btnTick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
cambioCompraValores();
insertarHistoricoCompraValores();
cambioSaldo();
}
});
return rootView;
}
Inside ScreenSlidePageFragment.java I have a method in which I call the value id_value but it loads it to zero.
int cantidadIntroducido = id_valor;
System.out.println("ESTAS EN: "+id_valor);