Tengo varias activitys que llaman a un mismo PagerAdapter para tener un layout deslizable en cada una de las actividades que la llamo. Mi intención es la de pasar un parámetro a demás de los que ya le paso. He añadido un elemento de tipo int al constructor y el Android Studio no me detecta ningún fallo, pero ala hora de recibir el valor tiene el mismo valor que cuando lo inicialize a 0. ¿que podría estar mal?
Actividad:
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;
}
Dentro de ScreenSlidePageFragment.java tengo un metodo en el que llamo al valor id_valor pero me lo carga a zero.
int cantidadIntroducido = id_valor;
System.out.println("ESTAS EN: "+id_valor);