0

I am using swipeview to swipe between two pages, each page is an activity by itself (an xml and a java file). Without the swipeview my app works fine and my database shows in each activity.

However, when I use the swipeview, the xml file shows (i.e.: the UI design is shown) and I can swipe between pages, but my java file doesn't seem to be linked (i.e.: all the functionalities are not there, including a database adapter) ..

here is my MainActivity.java:

Public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create and set adapter
        CustomPagerAdapter adapter = new CustomPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.customviewpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);
    }

}

and here is my activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/customviewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

and here is my cutomPagerAdapter file:

public class CustomPagerAdapter extends PagerAdapter {
    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int resId = 0;
        switch (position) {
        case 0: {
            resId = R.layout.activity_one;
            break;
        }
        case 1: {
            resId = R.layout.activity_two;
            break;
        }
        }
        View view = inflater.inflate(resId, null);
        ((ViewPager) collection).addView(view, 0);
        return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

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

how can I link my Java file to the UI xml file while using swipeview ?

McLan
  • 2,552
  • 9
  • 51
  • 85

1 Answers1

1

In this code you are not starting an activity, you are simply inflating a different layout.

    View view = inflater.inflate(resId, null);
    ((ViewPager) collection).addView(view, 0);
    return view;

When starting a new activity you should use startActivityForResult, something like

Intent intent = new Intent(MainActivity.this, SecondActivity.class);  
startActivityForResult(intent, 0);

Then in the onCreate method is where you should be setting the adapter or whatever you need to do to setup. Activities should be top level and self contained, not managed by an adapter.

EDIT You want to do something like: start your launch activity, this may be one of your activities that you are talking about. On swipe, start the second activity (not simply loading a layout), on swipe back start the first activity. If you don't want to keep the activities in stack (pressing the return arrow goes back to it) you can implement onActivityResult() to finish() the activity (close it). I suggest you look at some tutorials for starting a new activity then apply it to your code.

http://www.c-sharpcorner.com/UploadFile/ef3808/how-to-open-new-activity-on-click-button-by-existing-activi/

Dave S
  • 3,378
  • 1
  • 20
  • 34
  • Thanks for your reply. I've tried your solution, but it doesn't work, giving me an error at `MainActivity.this`. Maybe because i am newbie to java (and android development), but I still do not fully understand how to implement your solution. Do you want me to create an instance of the _MainActivity.java_ class and use it in my adapter and use these your 2 lines of code in my _case switch_!! .. – McLan May 21 '14 at 13:21
  • MainActivity.this is the context from which you want to create the intent. Where are you calling this? if you are calling it from the adapter you can pass the context to it from MainActivity. – Dave S May 21 '14 at 21:34
  • Yes .. I want to do exactly what you are describing in your edit. Even though I know how to move between different activities using a button but honestly I don't know how to implement it within swipeview .. I am just trying and trying and trying and nothing is working .. something is not clear for me and the frustrating part is that I don't even know what is it – McLan May 21 '14 at 22:38
  • I've never used swipeview and you don't have any example code using it, just a pageAdapter, which I have also never used. So it's hard to help you there. Can you show me the code where you are trying to start the new activity and the errors you are getting? – Dave S May 21 '14 at 22:59