The activities of starting Android in loop

advertisements

I have a SQLite Database with 3 records for example:
1 - Name1 - Do wash
2 - Name2 - Do this
3 - Name3 - Do that

Now I want to start one activity after another (for each record) when user presses a button. The Activity is always the same with other content.
So what is the best way to do this?

My thoughts are following:
- Make a for loop and use StartActivityForResult(...)
- Pass data throug all activities using intent
- Create a temporary table and read data in each activity
- Change only the content of the layout, but don't know how i could do this

So any suggestions on this?


I think for showing same type of view on multiple content you should use fragment with viewpager. I am just suggesting code to you for viewpager and fragment -

layout for content (pages.xml)-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/rel" >

    <TextView
        android:id="@+id/pageContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

Main activity content(activity_main.xml) -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/relativeTextview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/header"
        android:padding="5dp" >

        <android.support.v4.view.ViewPager
            android:id="@+id/reviewpager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </RelativeLayout>

</RelativeLayout>

Pager adapter for setting on viewpager(ViewPagerAdapter.java)-

public class ViewPagerAdapter extends PagerAdapter {
    int size;
    Activity act;
    View layout;
    TextView pagecontent;
    Button click;

    public ViewPagerAdapter(MainActivity mainActivity, int noofsize) {
        // TODO Auto-generated constructor stub
        size = noofsize;
        act = mainActivity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return size;
    }

    @Override
    public Object instantiateItem(View container, int position) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) act
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = inflater.inflate(R.layout.pages, null);
        pagecontent = (TextView) layout.findViewById(R.id.pageContent);
        int pagenumberTxt=position + 1;
        pagecontent.setText("Now your in Page No  " +pagenumberTxt );
        ((ViewPager) container).addView(layout, 0);
        return layout;
    }

    @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;
    }

    // }

}

Now in main activity((MainActivity.java) -

public class MainActivity extends Activity {

          int noofsize = 6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this,
                noofsize);
        ViewPager myPager = (ViewPager) findViewById(R.id.reviewpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

and do not forget to mention activity in manufest.xml. In pages.xml you can add the button on on click of this button you can change the view for next and previous(mPager.setCurrentItem(position)) using listener.