Show the first text of the ViewPager in Android Studio obtained from an array

1

I am doing a project that consists of the following: When the application starts, a list of countries is displayed, the code of that activity ...

public class MainActivity extends AppCompatActivity {

    ListView listView;

    Arrays arrays;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        arrays = new Arrays();

        listView = (ListView) findViewById(R.id.list_view);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrays.getPaises());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int posicion, long id) {                  
                Intent intent = new Intent(getApplicationContext(), ScreenSlidePagerActivity.class);
                intent.putExtra("posicion", posicion);
                startActivity(intent);
            }
        });
    }

}

the xml

asked by Felix A Marrero Pentón 16.09.2018 в 17:38
source

1 answer

0

The problem is that you set the position to the pager before adding the OnPageChangeListener. I recommend that you put the setPosition in the onResume and to be sure you can also add a postDelayed ()

int posicion = 0;

protected void onCreate(Bundle savedInstanceState) {

   posicion = bundle.getInt("posicion");
}

protected void onResume(){
    //De esta forma te aseguras que el pager este creado y con el listener seteado
    mPager.postDelayed(new Runnable() {
       @Override
       public void run() {
          mPager.setCurrentItem(posicion);
       }
    }, 100);
}    

//Esto va en la implementacion del OnPageChangeListener
@Override
public void onPageSelected(int i) {
   position = i;//actializo la posicion
}

I edit the answer to give you a code example of what I said in the comment.

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
   SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
   Array<String> paises = new Array();

   public ScreenSlidePagerAdapter(FragmentManager fm,Array<String> paises) {
      super(fm);
      this.paises = paises;
   }

   @Override
   public Fragment getItem(int position) {
      ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
      fragment.setPais(paise.get(position));
      return fragment;
   }

   public Fragment getRegisteredFragment(int position) {
      return registeredFragments.get(position);
   }

   @Override
   public int getCount() {
      return paises.count;
   }

   @Override
   public Object instantiateItem(ViewGroup container, int position){
      Fragment fragment = (Fragment) super.instantiateItem(container, position);
      registeredFragments.put(position, fragment);
      return fragment;
   }
}

I imagine that with this it will be clearer and you will be able to do it correctly.

    
answered by 16.09.2018 / 21:00
source