jayeshkv
jayeshkv

Reputation: 2208

Viewpager value doesnt change while getting the postion from the listview

Lets say this is the start page where the user selects the page he wants to see

    lv.setOnItemClickListener(new OnItemClickListener(
                ) {
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int position, long arg3) {
                        SharedPreferences preferences = getSharedPreferences("myprefs", Context.MODE_PRIVATE);
                        SharedPreferences.Editor   editor = preferences.edit();
                        editor.putInt("Position_content",position);
                        editor.commit();
                        Intent newpage = new Intent(getApplicationContext(),MainActivity.class);    
                        startActivity(newpage);
                        }});

and this is my Mainactivity.class

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        FragmentManager fm = getSupportFragmentManager();
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
        pager.setAdapter(pagerAdapter);
        sharedPref = getSharedPreferences("myprefs", Context.MODE_PRIVATE);
        content_selection_page = sharedPref.getInt("Position_content",0);
        SharedPreferences preferences = getSharedPreferences("myprefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor   editor = preferences.edit();
        editor.putInt("final_Position_content",content_selection_page);
        editor.commit();    }    

This is MyFragmentAdapter.java

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
    public static final String PREFS_NAME = "MyPrefsFile";
    final int PAGE_COUNT = 52;

    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);  }

    @Override
    public Fragment getItem(int arg0) {     
        MyFragment myFragment = new MyFragment();
        Bundle data = new Bundle();
        data.putInt("current_page", arg0+1);
        myFragment.setArguments(data);
        return myFragment;
    }
    @Override
    public int getCount() {     
        return PAGE_COUNT;
    }

    @Override
    public CharSequence getPageTitle(int position) {        
        return "Page " + ( position + 1 );  
    }   

}

This is the Fragment.java

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle data = getArguments();
        //** Getting integer data of the key current_page from the bundle *//*
        mCurrentPage = data.getInt("current_page", 0);
        sharedPref = this.getActivity().getSharedPreferences("myprefs", Context.MODE_PRIVATE);
        content_selection_page = sharedPref.getInt("final_Position_content",0);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.myfragment_layout, container,false);             
        TextView sub = (TextView ) v.findViewById(R.id.sub1);
        TextView sub1 = (TextView ) v.findViewById(R.id.sub2);
        TextView hd = (TextView ) v.findViewById(R.id.head);
        content_selection_page++;  //doesnt work


        switch(content_selection_page){
                switch stmts.....
                }
                return v;
                } 

The Real Problem Now the problem is the user selects a choice through a list and the position is got to the switch case in the fragment.java now the viewpager doesnt seem to work. suppose if i click the list item 4 it takes me to the 3rd page and when i slide it slides but it still is in the same page, how do i make it such that if i slide once it has to increment the page everytime. Diagramatically instead of Content 2 i keep getting content 1

Upvotes: 2

Views: 592

Answers (1)

jayeshkv
jayeshkv

Reputation: 2208

How to setCurrentPage in ViewPager according to user selection & how do I know which list item called the activity

that link helped me :) looks like i missed mPager.setCurrentItem(position);

Upvotes: 3

Related Questions