I am testing out the FragmentPagerAdapter and I had it all in a single class before. And everything worked, but once I separated SectionsPagerAdapter class, the getString doesn't work under the getPageTitle function.
I know getPageTitle is part of the PagerAdapter class, but I want to know what the best way to have that function included in this class. Do I need to extend the class?
SectionsPageAdapter class
import java.util.Locale;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
// A FragmentPagerAdapter that returns a fragment corresponding to one of the sections/tabs/pages.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.myFriendsTab).toUpperCase(l);
case 1:
return getString(R.string.myDealsTab).toUpperCase(l);
case 2:
return getString(R.string.featuredDealsTab).toUpperCase(l);
case 3:
return getString(R.string.browseCategoriesTab).toUpperCase(l);
case 4:
return getString(R.string.localDealsTab).toUpperCase(l);
}
return null;
}
#Override
public int getCount() {
// Show 5 total pages.
return 5;
}
}
MainActivity class
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
// Fragment PagerAdapter keeps every loaded fragment in memory.
// If too memory intensive, switch to FragmentStatePagerAdapter.
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager; // ViewPager that will host section contents.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creates the adapter that will return a fragment for each of the primary sections.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), null);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
getString(int) only works for Classes that have access to a Context - Fragments, Activities, etc.
Given that this an Adapter class, it won't have direct access to a Context, so you should probably pass one in with the constructor.
private Context mContext = null;
public SectionsPagerAdapter(FragmentManager fm, Context context) {
super(fm);
mContext = context;
}
and then use the member field to access getString(int)
return mContext.getString(R.string.myFriendsTab).toUpperCase(1);
as panini said, getString method need to be called on Context, follow these steps :
Step 1 : in the adapter class, create a field to store the Context on it.
private Context mContext
Step 2 : in the adapter class, adjust the constructor to pass the Context as the first parameter.
public SectionsPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
Step 3 : in the adapter class, and inside the getPageTitle method, call the getString on the Context field mContext,
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return mContext.getString(R.string.myFriendsTab).toUpperCase(l);
case 1:
return mContext.getString(R.string.myDealsTab).toUpperCase(l);
case 2:
return mContext.getString(R.string.featuredDealsTab).toUpperCase(l);
case 3:
return mContext.getString(R.string.browseCategoriesTab).toUpperCase(l);
case 4:
return mContext.getString(R.string.localDealsTab).toUpperCase(l);
}
return null;
}
Step 4 : wherever you used the adapter adjust it to include the Context parameter as we defined in the constructor.
in MainActivity class, adjust the constructor to be like this :
mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
Related
In my MainActivity with ActionBar I listen to a listener from a DialogFragment and based on which Fragment of ActionBar i am on, I need to do some things.
I am using below code to get the current fragment and check which of the two ActionBar fragment i am on:
private Fragment getVisibleFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
List<Fragment> fragments = fragmentManager.getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
if (fragment != null && fragment.isVisible())
return fragment;
}
}
return null;
}
but fragment.isVisible() returns true for both the fragments. Why would that be? Is there another flag I need to consider?
Below is my FragmentPagerAdapter implentation:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
Context context;
public SectionsPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new ReceivedListFragment();
case 1:
return new SentListFragment();
}
return null;
}
#Override
public int getCount() {
// Show 2 total pages.
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return context.getString(R.string.title_section1).toUpperCase(l);
case 1:
return context.getString(R.string.title_section2).toUpperCase(l);
}
return null;
}
}
It seems you are using a ViewPager and you need to find out which is the currently selected view. The problem in your approach, in my opinion, is that the viewpager keeps instance of at least two fragments. It seems you have two fragments only, both of the fragments are kept by the viewpager and which is why you get the isVisible true for both the fragments.
If your only need is to get the currently selected fragment on the viewpager, please try this method from the viewpager class:
mViewPagerInstance.getCurrentItem();
which will give you the currently selected fragment index.
Another way would be to add a PageChangeListener for your viewpager and then listen to page change events:
class MyActivity extends Activity{
private int curPage;
private static class MFragChangeListener extends SimpleOnPageChangeListener{
public void onPageSelected(int position) {
curPage = position;
}
}
........... /* Rest of your code */
mViewPagerInstance.setOnPageChangeListener(new MFragChangeListener());
/* Now access curPage to get the current selected page index whenever you need it. */
Please refer to this SO post as well.
I have a ViewPager that works well, but I want to make it a little better.
Right now in my PagerAdapter class, in the getItem method I use a switch statement:
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragmentChicken tabChicken = new TabFragmentChicken();
return tabChicken;
case 1:
TabFragmentFish tabFish = new TabFragmentFish();
return tabFish;
...
And then over in my activity class, I set the tabs name like so:
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Chicken"));
tabLayout.addTab(tabLayout.newTab().setText("Fish"));
...
Is there a way I can better modularize this? For instance if I wanted to change the order of the tabs I'd have to change both the switch statement and the tab names. Is there another class or structure I can use to keep it all in one place so I only need to update one thing and not have to bother with the switch/adding tab text changes?
You can create a custom Object that will hold the title and the fragment, then use a list of this Object within your adapter. Here is a small way of implementing it:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.chahinemouhamad.parrrot.home.recent.RecentFragment;
public class HomePagerAdapter extends FragmentPagerAdapter {
private final PagerFragment[] pagerFragments;
public HomePagerAdapter(final FragmentManager fm) {
super(fm);
pagerFragments = new PagerFragment[] {
new PagerFragment("item 1", RecentFragment.newInstance()),
new PagerFragment("item 2", RecentFragment.newInstance())
};
}
#Override public Fragment getItem(final int position) {
return pagerFragments[position].fragment;
}
#Override public int getCount() {
return pagerFragments.length;
}
#Override public CharSequence getPageTitle(final int position) {
return pagerFragments[position].title;
}
class PagerFragment {
public final String title;
public final Fragment fragment;
public PagerFragment(String title, Fragment fragment) {
this.title = title;
this.fragment = fragment;
}
}
}
I have been at this for about 3 days looking up various websites for reference, but I am stuck. I am using the swipe and tabs layout which give you the standard dummy section fragment. But I have 3 separate fragments that I want to display different .xml layouts for. This is the section of code that is bothering me:
import java.util.Locale;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Fragment fragment2 = new DummySectionFragment2();
Fragment fragment3 = new DummySectionFragment3();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
args.putInt(DummySectionFragment2.ARG_SECTION_NUMBER, position + 2);
args.putInt(DummySectionFragment3.ARG_SECTION_NUMBER, position + 3);
fragment.setArguments(args);
fragment2.setArguments(args);
fragment3.setArguments(args);
return fragment2;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
public static class DummySectionFragment2 extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment2() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView2 = inflater.inflate(R.layout.fragment_2, container, false);
return rootView2;
}
}
public static class DummySectionFragment3 extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number_3";
public DummySectionFragment3() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView3 = inflater.inflate(R.layout.fragment_3, container, false);
return rootView3;
}
}
}
This returns fragment 2. I want to display 3 fragments when each tab is selected. How do I do it? I'm guessing that it's what is returning right?
edit.. ive included the whole java class.. in the hope i can resove this.. How do I use FragmentPagerAdapter to have tabs with different content? ive just tried the following (Second Answer as i didnt understnd what was meant in the first) it didnt work
Use the method this way:
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
switch (position) {
case 0:
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(fragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
case 1:
Fragment fragment2 = new DummySectionFragment2();
Bundle args = new Bundle();
args.putInt(fragment2 .ARG_SECTION_NUMBER, position + 2);
fragment2.setArguments(args);
return fragment2;
case 2:
Fragment fragment3 = new DummySectionFragment3();
Bundle args = new Bundle();
args.putInt(fragment3.ARG_SECTION_NUMBER, position + 3);
fragment3.setArguments(args);
return fragment3;
default:
return null;
}
}
My First App
I am trying to use Swipe + Tiles Navigation in android . I want to show a different string on each fragment / or tile . I used switch case to use a different string based on int position . The app checked out with no errors but keeps on crashing when i run it . Tried using if else but now i am stuck . Help would be deeply appreciated .
MainActivity.java
package com.example.swipetile;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
int position; // Setting global variables .. trying to solve the issue at the end
String ARG_SECTION_NUMBER;
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#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;
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 5);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase();
case 1:
return getString(R.string.title_section2).toUpperCase();
case 2:
return getString(R.string.title_section3).toUpperCase();
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create a new TextView and set its text to the fragment's section
// number argument value.
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
/*
textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return textView;
*/
// Original Procedure
/*
textView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER))); return textView;
*/
// public CharSequence getPageTitle(int position)
CharSequence getItem(int position) {
if (position == 0) {
textView.setText(getString(R.string.hello_world));
return textView;
} else if (position == 1) {
textView.setText(getString(R.string.hello_world));
return textView;
} else if (position == 2) {
textView.setText(getString(R.string.hello_world));
return textView;
} else if (position == 3) {
textView.setText(getString(R.string.hello_world));
return textView;
} else {
textView.setText(getString(R.string.hello_world));
return textView;
}
// the one that had no errors but closed
/*
int position = Integer.parseInt(ARG_SECTION_NUMBER);
switch (position)
{
case 1:
textView.setText(getString(R.string.hello_world));
return textView;
case 2:
textView.setText(getString(R.string.hello_world));
return textView;
case 3:
textView.setText(getString(R.string.hello_world));
return textView;
default:
textView.setText(getString(R.string.hello_world));
return textView;
}
*/
/*textView.setText(getString(R.string.hello_world));
return textView;
*/
}
}
}
I am tried commenting out the position int at the top and the string ARG_SECTION_NUMBER;
and then tried using the switch statement .. same result .. no error .. app just dies when i run it .
LOGCAT ERROR SHOT
http://tinypic.com/r/1zmgo55/5
Thanks in advance
You are trying to parse ARG_SECTION_NUMBER in to an Integer which is a string.
If you are using eclipse, you can check for the exception logs in Logcat.
Window > Show View > Other > Logcat
EDIT:-
You are getting NumberFormatException because of ARG_SECTION_NUMBER.
add these lines in onCreateView() of DummySectionFragment
Bundle bundle=getArguments();
int position = bundle.getInt(ARG_SECTION_NUMBER);
I'm very new to Android, so forgive me if this is a terrible question, but I've searched high and low and I've been reading lots of material and can't seem to figure this out. I've created an app in Eclipse using one of the default views (fixed tabs + swipe). I created a second class with a listview and I'm trying to add this class to load in one of the tabs.
EDIT to include full MainActivity.java
package com.sonnyparlin.gracietampa;
import java.util.Locale;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Html.fromHtml(getString(R.string.page1text)));
} else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
// I want to add my listview here
} else {
rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
}
return rootView;
}
}
}
My TechniqueActivity.java file:
public class TechniqueActivity extends ListActivity{
public TechniqueActivity() {
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] technique_list = getResources().getStringArray(R.array.technique_list);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, technique_list));
}
}
I would really appreciate it if someone could point me in the right direction so that I can populate the second tab of my application with the listview I've created in TechniqueActivity.java. Or maybe there's a completely different / better way of doing it?
Okay, after more research and playing around, I found the solution, which was basically what Rarw said in the second part of his answer, which was to use a Fragment instead of an Activity. So, I got rid of TechniqueActivity.java and created a TechniqueFragment instead. Then I call it in the getItem().
package com.sonnyparlin.gracietampa;
import java.util.Locale;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment;
if (position == 0 || position == 2) {
fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
} else {
fragment = new TechniqueFragment();
//setContentView(R.layout.technique_activity);
}
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Html.fromHtml(getString(R.string.page1text)));
} else {
rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
}
return rootView;
}
}
public static class TechniqueFragment extends Fragment {
public TechniqueFragment() {
// TODO Auto-generated constructor stub
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView;
rootView = inflater.inflate(R.layout.technique_activity,
container, false);
ListView lv1 = (ListView) rootView.findViewById(R.id.ListView01);
String[] technique_list = getResources().getStringArray(R.array.technique_list);
lv1.setAdapter(new ArrayAdapter<String>(lv1.getContext(), R.layout.list_item, technique_list));
return rootView;
}
}
}
It doesn't look like you're actually using any tabs. Check out this tutorial for the basic idea. If you're going to use tabs you need to (1) create the tabs and then (2) set your activity to be loaded by that tab. The code above just looks like you're creating one activity and then inflating that layout. The blog post provides a good overview of a simple 3 tab application which you can adapt to your case.
The other way to do this would be to use a ViewPager which you can read more about here. The ViewPager approach uses Fragment and not Acitivity. Basicly you create a ViewPager and an adapter which you then add instances of your fragments to. As you swipe the ViewPager rotates through the fragments in the order you add them to the adapter.
Those look like the 2 most relevant ways to approach this situation.