Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to build an book application. currently my application has two activities first one is "MainActivity" on which I'm write content list. and second is "ShowPage" activity.I'm using ShowPage activity for showing fragments thats I'm using like pages. In the MainActivity I use List View for contents. My problem is that when I click on first content then first page is open as I want but when I click on third or forth or fifth content then first page also open while I want other page like fifth or someone other. Please help me to find out solution of this problem. MainActivity code, ShowPage Activity code, and adapter code is below.

MainActivity Code.

package com.exe.testbook;

import ...

public class MainActivity extends AppCompatActivity {

    ListView listView;
    String[] surah = {"C01","C02","C03","C04","C05","C06","C07","C08","C09"};

    private DrawerLayout dl;
    private ActionBarDrawerToggle abdt;

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

        //list view portion
        listView = findViewById(R.id.list_view);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_dropdown, surah);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View v, int i, long l) {
                switch (i)
                {
                    case 0:
                        Intent intent = new Intent(MainActivity.this, ShowPage.class);
                        startActivity(intent);
                        break;
		    case 1:
                        Intent intent = new Intent(MainActivity.this, ShowPage.class);
                        startActivity(intent);
                        break;
		    case 2:
                        Intent intent = new Intent(MainActivity.this, ShowPage.class);
                        startActivity(intent);
                        break;
                }
            }
        });



        //navigation view portion
        dl = (DrawerLayout) findViewById(R.id.dl);
        abdt = new ActionBarDrawerToggle(this, dl, R.string.open,R.string.close);
        abdt.setDrawerIndicatorEnabled(true);
        dl.addDrawerListener(abdt);
        abdt.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        NavigationView nav_view = (NavigationView) findViewById(R.id.nav_view);
        nav_view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id = item.getItemId();
                if (id==R.id.marker){
                    Toast.makeText(MainActivity.this, "'Marker' is clicked.", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        if (abdt.onOptionsItemSelected(item)) return true;
        if (super.onOptionsItemSelected(item)) return true;
        return false;
    }
}


ShowPage Activity Code.

package com.exe.testbook;

import ...

public class ShowPage extends AppCompatActivity {

    ViewPager viewPager;

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

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        viewPager = findViewById(R.id.viewPager);

        if (viewPager != null){
            viewPagerAdapter adapter = new viewPagerAdapter(getSupportFragmentManager());
            viewPager.setAdapter(adapter);
        }


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


What I have tried:

Adapter Code.

package com.exe.testbook;

import ...

public class viewPagerAdapter extends FragmentPagerAdapter {

    private int COUNT = 10;

    public viewPagerAdapter(@NonNull FragmentManager fm) {
        super(fm);
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {

        Fragment fragment = null;

        switch (position){

            case 0:
                fragment = new Page1();
                break;
            case 1:
                fragment = new Page2();
                break;
            case 2:
                fragment = new Page3();
                break;
            case 3:
                fragment = new Page4();
                break;
            case 4:
                fragment = new Page5();
                break;
            case 5:
                fragment = new Page6();
                break;
            case 6:
                fragment = new Page7();
                break;
            case 7:
                fragment = new Page8();
                break;
            case 8:
                fragment = new Page9();
                break;
            case 9:
                fragment = new Page10();
                break;

        }

        return fragment;
    }

    @Override
    public int getCount() {
        return COUNT;
    }

}
Posted
Updated 10-Feb-23 6:59am
Comments
wseng 9-Feb-23 12:14pm    
In MainActivity, why you Intent to same class?
Mehboob Shaukat 9-Feb-23 14:11pm    
Excuse me, but what should I do? And how?
wseng 9-Feb-23 21:32pm    
I do not really understand your requirements without seeing your UI design. Is it possible to show us your UI and elaborate more on your issue?
Mehboob Shaukat 9-Feb-23 22:13pm    
why not! Currently I'm having trouble attaching the image but I'll try the other answer to show you a screenshot of the UI. But the problem is suppose i am designing app for story books and c01 is name of first story content and c02 for second story content and so on. The first story is of 4 pages and the next story is starting from the 5th page. I want when I click on c01 the first page should open which is correct but when I click on c02 the 5th page should open but it doesn't. Even when I click on c02 the first page opens. One thing I'm wondering is if there is a Go to Page option like in the Books app. Maybe now I can explain my problem and I hope you understand.
Sorry for long reply but I try to describe my problem. Thanks for reading it.
Mehboob Shaukat 10-Feb-23 0:46am    
How can I show you UI Please tell me?

1 solution

You can pass a variable to ShowPage, and use setCurrentItem to set the viewPager index.

In MainActivity
case 0:
    Intent intent = new Intent(MainActivity.this, ShowPage.class);
    intent.putExtra("position",0); // add this line
    startActivity(intent);
    break;
case 1:
    Intent intent1 = new Intent(MainActivity.this, ShowPage.class);
    intent1.putExtra("position",5);  // add this line
    startActivity(intent1);
    break;

In ShowPage
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

Intent intent = getIntent();  // add this line
int  data = intent.getIntExtra("position",0); // add this line
viewPager = findViewById(R.id.viewPager);

if (viewPager != null){
    viewPagerAdapter adapter = new viewPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(data);  // add this line
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900