FragmentStatePagerAdapter - getItem - android

I know there are several other posts on this topic, but I wanted to paste my code because I believe there may be an error in it that is causing my problem. My FragmentStatePagerAdapter is returning the wrong position, and I am not sure why.
Here is the code in my Main activity.
Main.java
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.microsoft.windowsazure.notifications.NotificationsManager;
import java.util.ArrayList;
public class Main extends AppCompatActivity {
public static TextView txtViewHeading;
public static TextView tab1TabBadge;
public static TextView tab2TabBadge;
public static TextView tab3TabBadge;
public static TextView tab4TabBadge;
public static TextView tab5TabBadge;
public static Button btnBack;
public static ImageButton btnShare;
public static Main mainActivity;
public static Context mainActivityContext;
public static Boolean isVisible = false;
private FragmentTabHost mTabHost;
private GoogleCloudMessaging gcm;
private ActionBar actionBar;
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private TabGroupAdapter mTabGroupAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up Push notifications
mainActivity = this;
mainActivityContext = Main.this;
NotificationsManager.handleNotifications(this, NotificationSettings.SenderId, PushHandler.class);
registerWithNotificationHubs();
// Set up the views for each tab - custom view used for Badge icon
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Bind the Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Set up the ActionBar
actionBar = getSupportActionBar();
if (actionBar!=null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
// Create the adapter that will return a fragment for each of the primary "tabs"
mTabGroupAdapter = new TabGroupAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mTabGroupAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
// tab1 Tab
View tab1TabView = inflater.inflate(R.layout.tab, null);
ImageView tab1TabIcon = (ImageView) tab1TabView.findViewById(R.id.tabIcon);
tab1TabIcon.setImageResource(R.drawable.tab_first);
TextView tab1TabText = (TextView) tab1TabView.findViewById(R.id.tabText);
tab1TabText.setText("tab1");
tab1TabText.setTypeface(arialTypeface);
tab1TabBadge = (TextView) tab1TabView.findViewById(R.id.tabBadge);
tab1TabBadge.setTypeface(arialTypeface);
tabLayout.getTabAt(0).setCustomView(tab1TabView);
// Also set this tab as Active be default
tabLayout.getTabAt(0).getCustomView().setSelected(true);
mViewPager.setCurrentItem(0);
// tab2 Tab
View tab2TabView = inflater.inflate(R.layout.tab, null);
ImageView tab2TabIcon = (ImageView) tab2TabView.findViewById(R.id.tabIcon);
tab2TabIcon.setImageResource(R.drawable.tab_second);
TextView tab2TabText = (TextView) tab2TabView.findViewById(R.id.tabText);
tab2TabText.setText("Odometer");
tab2TabText.setTypeface(arialTypeface);
tab2TabBadge = (TextView) tab2TabView.findViewById(R.id.tabBadge);
tab2TabBadge.setTypeface(arialTypeface);
tabLayout.getTabAt(1).setCustomView(tab2TabView);
// tab3 Tab
View tab3TabView = inflater.inflate(R.layout.tab, null);
ImageView tab3TabIcon = (ImageView) tab3TabView.findViewById(R.id.tabIcon);
tab3TabIcon.setImageResource(R.drawable.tab_third);
TextView tab3TabText = (TextView) tab3TabView.findViewById(R.id.tabText);
tab3TabText.setText("tab3");
tab3TabText.setTypeface(arialTypeface);
tab3TabBadge = (TextView) tab3TabView.findViewById(R.id.tabBadge);
tab3TabBadge.setTypeface(arialTypeface);
tabLayout.getTabAt(2).setCustomView(tab3TabView);
// tab4 Tab
View tab4TabView = inflater.inflate(R.layout.tab, null);
ImageView tab4TabIcon = (ImageView) tab4TabView.findViewById(R.id.tabIcon);
tab4TabIcon.setImageResource(R.drawable.tab_fourth);
TextView tab4TabText = (TextView) tab4TabView.findViewById(R.id.tabText);
tab4TabText.setText("tab4");
tab4TabText.setTypeface(arialTypeface);
tab4TabBadge = (TextView) tab4TabView.findViewById(R.id.tabBadge);
tab4TabBadge.setTypeface(arialTypeface);
tabLayout.getTabAt(3).setCustomView(tab4TabView);
// tab5 Tab
View tab5TabView = inflater.inflate(R.layout.tab, null);
ImageView tab5TabIcon = (ImageView) tab5TabView.findViewById(R.id.tabIcon);
tab5TabIcon.setImageResource(R.drawable.tab_fifth);
TextView tab5TabText = (TextView) tab5TabView.findViewById(R.id.tabText);
tab5TabText.setText("tab5");
tab5TabText.setTypeface(arialTypeface);
tab5TabBadge = (TextView) tab5TabView.findViewById(R.id.tabBadge);
tab5TabBadge.setTypeface(arialTypeface);
tabLayout.getTabAt(4).setCustomView(tab5TabView);
// Tab listener
/*
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
txtViewHeading.setText(tab.getText());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
*/
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
if(mViewPager.getCurrentItem()==0){
onBackPressed();
}
else{
mViewPager.setCurrentItem(mViewPager.getCurrentItem()-1);
}
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPause() {
super.onPause();
isVisible = false;
}
#Override
protected void onResume() {
super.onResume();
isVisible = true;
}
#Override
protected void onStop() {
super.onStop();
isVisible = false;
}
private boolean checkPlayServices() {
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (apiAvailability.isUserResolvableError(resultCode)) {
apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
.show();
} else {
ToastNotify("This device is not supported by Google Play Services.");
finish();
}
return false;
}
return true;
}
public void ToastNotify(final String notificationMessage) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(Main.this, notificationMessage, Toast.LENGTH_LONG).show();
}
});
}
private void registerWithNotificationHubs()
{
if (checkPlayServices()) {
// Start IntentService to register this application with GCM.
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}
}
...and here is the code for my FragmentStatePagerAdapter
TabGroupAdapter
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.Log;
import java.util.ArrayList;
public class TabGroupAdapter extends FragmentStatePagerAdapter {
private static int fragmentSize = 5;
private static ArrayList<String> fragmentTitles = new ArrayList<String>() {
{
add("tab1");
add("tab2");
add("tab3");
add("tab4");
add("tab5");
}
};
public TabGroupAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Log.d("tag","POSITION: " + position);
switch (position) {
case 0:
default:
return new Tab1Fragment();
case 1:
return new Tab2Fragment();
case 2:
return new Tab3Fragment();
case 3:
return new Tab4Fragment();
case 4:
return new Tab5Fragment();
}
}
#Override
public int getCount() {
return fragmentSize;
}
#Override
public CharSequence getPageTitle(int position) {
return fragmentTitles.get(position);
}
}
To sum it up, when my app loads, the first tab is empty. Just a blank screen. It is not my Tab1Fragment as it should be. Also when I click through my tabs, the position should show from left to right 0,1,2,3,4 as I have 5 tabs. It does not though. I have a Log.d statement in my getItem(position) method in which I am logging the current position to the console. When my app loads it shows Position:0, then Position:1 all on load, which is weird since it should only show position 0 since that is the default tab (tab1). Then when I click through, tab 2 shows position 2, tab 3 shows position 3, tab 4 shows position 4 and tab 5 does not log at all. Then working back from tab 5 to tab 1, the positions are all out of whack, and when I get to tab 1 it does not log. It's quite strange. Also, my back arrow in my fragment that SHOULD only show up if I am not at the root fragment of the tab, instead shows up all the time.
Can anyone please help me figure out why this is happening?
If any more code is needed to determine the problem, I am happy to post it just please let me know what you need to see.

Try commenting out all of the code beneath:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
in your onCreate method. Run again and see what happens.
Your switch statement is laid out in a strange way, too. If you'll definitely only have 5 cases, then it should go from case 0-4, with a default case at the end that prints an error to the log, or throws an exception. I don't think this will be causing your issue, but it's good practice.
Also it's usually not a good idea to instantiate fragments using the default constructor. A better way is to use a static factory method, ie:
Tab1Fragment fragment = Tab1Fragment.create();
return fragment;
With a static create() method in your fragment, that initialises the object. It gives you a single access point to fragment, and means that any initialisation you need to do (now or at some point down the line) can all be done in once place, making you less prone to error. Android also re-creates fragments using the default constructor, so any initialisation you come to do in an overloaded constructor will be ignored. Again I don't think this is the cause of your issue, but it's worth bearing in mind.
Edit: In addition, when you say it is choosing random fragments "The position will show 0,3,2,0,4,1 just random numbers", do you mean those are the fragments being displayed on screen, or are those the positions you're logging from the "getItem(int position)" argument?

viewPager = (ViewPager) findViewById(R.id.tabPager);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Fragment1(),"fragment 1");
adapter.addFragment(new Fragment2(),"fragment 2");
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
return position;
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
assert tabLayout != null;
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
Then in your Adapter
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment,String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}

Related

Communication between swipe view Tabs

i followed this article http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/ to implement swipe view and it work nice. So now i want to add button on Tab1 and when clicked has to send data to Tab2.
I know there is the issue of using interface but honestly i don't know how to add it on these codes and work provide am completely new to the android.
so far i have tried my best on these links Communication between SlidingTabLayout tabs, How to pass data from one swipe tab to another? and How can I communicate/pass data through different Fragments with Swipe Tab? but i fail.
Any one help please to make it work on every stage , i mean from Tab1 ->Mainactivity->Tab2
thanks alot
Here are the codes after i edit the question
Fragment1
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TabFragment1 extends Fragment implements AdapterView.OnItemSelectedListener{
Button send;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.tab_fragment_1, container, false);
send=(Button)v.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// value to be sent to the TabFragment2 on button click
String value=" My Data";
}
});
return v;
}
}
Fragment2
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TabFragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab_fragment_2, container, false);
//Display value from TabFragment1
textview.setText(value);
}
}
Interface class
public Interface FragmentCommunication
{
public void printMessage(String message);
}
MainActivity
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity implements FragmentCommunication{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public void printMessage(String message)
{
Toast.makeText(MainActivity.this,message,Toast.LENGTH_LONG).show();
}
}
Adapter Class
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
so after posting more code I'm answering:
Implement some FragmentCommunication for both your Fragments:
//public class TabFragment1 extends Fragment implements FragmentCommunication{
public class TabFragment2 extends Fragment implements FragmentCommunication{
//public static final String TAG="TabFragment1";
public static final String TAG="TabFragment2";
...
#Override
public void onActivityCreated (Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
if(getActivity() instanceOf MainActivity)
((MainActivity) getActivity()).registerFragmentCommunication(
TAG, this);
}
#Override
public void printMessage(String message){
Toast.makeText(getActivity(), TAG+" says: "+message,
Toast.LENGTH_SHORT).show();
}
}
note this line inside onActivityCreated:
((MainActivity) getActivity()).registerFragmentCommunication(TAG, this);
this means implemented FragmentCommunication, TAG will be unique key/name of Fragment and whole method is registration of your interface in Activity. so now you have to add registerFragmentCommunication method to your MainActivity and keep reference to passed interfaces, e.g. in HashMap:
HashMap<String, FragmentCommunication> fragmentCommunications =
new HashMap<String, FragmentCommunication>();
...
public void registerFragmentCommunication(String key, FragmentCommunication fc){
fragmentCommunications.put(key, fc);
}
so now you can access each FragmentCommunication of both fragments from your MainActivity, e.g by using method like this:
public void callFragmentCommunication(String key, String msg){
if(fragmentCommunications.get(key)!=null)
fragmentCommunications.get(key).printMessage();
}
callFragmentCommunication(TabFragment1.TAG, "hi!");
callFragmentCommunication(TabFragment2.TAG, "hi!");
method is public, so Fragments can call it simply like this:
//inside TabFragment1
if(isAdded() && getActivity()!=null &&
getActivity() instanceOf MainActivity &&
!getActivity().isFinishing())
((MainActivity) getActivity()).callFragmentCommunication(
TabFragment2.TAG, "hi!");
this will show Toast with text: "TabFragment2 says: hi!", which was called inside TabFragment1. now you may pass another kind of data as well :)
there is much more methods like communicating through FragmentStatePagerAdapter like here, passing Bundle arguments where fragments are initialized, additional feedback interfaces (e.g. returning true/false when message was passed) etc. Above is just very simplified way, good luck with improving it! :)

Tabs with Icons using ViewPager, How to layout them? by Attaching Some Layout File

I am new to Android, Designing an Android Application that contains "Tabs".
I have designed this:
But I want to design like this,
I think there must be way, as like of populating lists using Custom Adapters, But I am not finding any thing to do this, the tutorials that are available on Android Web are out of scope of my mind, because I am new..
My code:
Tabs.xml
Tabs.java
package com.example.medbleep;
import TabsManager.TabPagerAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.TextView;
public class Tabs extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabPagerAdapter mAdapter;
private android.app.ActionBar actionBar;
private int numberOfTabs = 5;
// Tab titles
private int[] tabIcons = {R.drawable.home,R.drawable.home,R.drawable.home,R.drawable.home,R.drawable.home,R.drawable.home};
private String[] tabText = { "Home", "units", "conferences", "couses", "more" };
ImageView imageView;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
imageView = (ImageView)findViewById(R.id.tab_icon);
textView = (TextView)findViewById(R.id.tab_title);
// Initialization
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setBackgroundColor(getResources().getColor(R.color.darkLoginBlue));
actionBar = getActionBar();
mAdapter = new TabPagerAdapter(getSupportFragmentManager(),
numberOfTabs);
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (int i = 0; i<5 ; i++) {
actionBar.addTab(actionBar.newTab().setText(tabText[i])
.setIcon(getResources().getDrawable(tabIcons[i]))
.setTabListener(this));
//actionBar.addTab(actionBar.newTab().setCustomView(R.layout.tab_layout).setText(tabText[i]).setIcon(tabIcons[i]).setTabListener(this));
//actionBar.setLogo(tabIcons[4]);
//actionBar.setIcon(tabIcons[i]);
//actionBar.setTitle(tabText[i]);
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
TabPagerAdapter.java
package TabsManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class TabPagerAdapter extends FragmentStatePagerAdapter{
int mNumOfTabs;
public TabPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
HomeTabFragment homeTabFragment = new HomeTabFragment();
return homeTabFragment;
case 1:
UnitTabFragment unitTabFragment = new UnitTabFragment();
return unitTabFragment;
case 2:
ConferencesTabFragment conferencesTabFragment = new ConferencesTabFragment();
return conferencesTabFragment;
case 3:
CoursesTabFragment coursesTabFragment = new CoursesTabFragment();
return coursesTabFragment;
case 4:
MoreTabFragment moreTabFragment = new MoreTabFragment();
return moreTabFragment;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
For making this, your have to create Custom Adaptor and set width and height According to your requirement of each tab.
Please Follow this:
http://mobikul.com/make-custom-tabs-icons-android/

Android sliding tabs, Fragment_Collection_Object Error

am following the Android Developers website to add sliding tabs to my Android app for my project, I am stuck with this one error and do not understand the problem, I know it has something to do with calling the correct Fragment layout, I am doing exactly what the tutorial tells me too, have any of you guys got any information to solve this, thank you.
package com.test.finalproject;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
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.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements View.OnClickListener {
DemoCollectionPagerAdapter mCollectionPagerAdapter;
ViewPager mViewPager;
Button buttonLogin;
Button buttonRegister;
Button buttonTheme;
Button buttonMaps;
#Override
protected void onCreate(Bundle savedInstanceState) {
Utils.setThemeToActivity(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final android.app.ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create a tab listener that is called when the user changes tabs.
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// probably ignore this event
}
};
// Add 3 tabs, specifying the tab's text and TabListener
for (int i = 0; i < 3; i++) {
actionBar.addTab(
actionBar.newTab()
.setText("Tab " + (i + 1))
.setTabListener(tabListener));
}
buttonLogin = (Button)findViewById(R.id.buttonLogin);
buttonLogin.setOnClickListener(this);
buttonRegister = (Button)findViewById(R.id.buttonRegister);
buttonRegister.setOnClickListener(this);
buttonTheme = (Button)findViewById(R.id.buttonTheme);
buttonTheme.setOnClickListener(this);
buttonMaps = (Button)findViewById(R.id.buttonMaps);
buttonMaps.setOnClickListener(this);
mCollectionPagerAdapter =
new DemoCollectionPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCollectionPagerAdapter);
}
public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = new DemoObjectFragment();
Bundle args = new Bundle();
// Our object is just an integer :-P
args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return 100;
}
#Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position + 1);
}
}
public static class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// The last two arguments ensure LayoutParams are inflated
// properly.
View rootView = inflater.inflate(
R.layout.fragment_collection_object, container, false);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText(
Integer.toString(args.getInt(ARG_OBJECT)));
return rootView;
}
}
public void onCreate1(Bundle savedInstanceState) {
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// When swiping between pages, select the
// corresponding tab.
getActionBar().setSelectedNavigationItem(position);
}
});
}
private void buttonLoginClick()
{
startActivity (new Intent("MainApp"));
}
private void buttonRegisterClick()
{
startActivity (new Intent("TextToo"));
}
private void buttonThemeClick()
{
startActivity (new Intent("SettingsTheme"));
}
private void buttonMapsClick()
{
startActivity (new Intent("GoogleMaps"));
}
#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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.buttonLogin:
buttonLoginClick();
break;
}
switch (v.getId())
{
case R.id.buttonRegister:
buttonRegisterClick();
break;
}
switch (v.getId())
{
case R.id.buttonTheme:
buttonThemeClick();
break;
}
switch (v.getId())
{
case R.id.buttonMaps:
buttonMapsClick();
break;
}
}
}
The Error is on line 126.
The Error is: Description Resource Path Location Type
fragment_collection_object cannot be resolved or is not a field MainActivity.java /Login/src/com/test/finalproject line 126 Java Problem
The problem you are facing is because of the missing xml file in the layout folder of project/res
Go to http://developer.android.com/training/implementing-navigation/lateral.html
Click on "download the sample app" and unzip it. Add the EffectiveNavigation\res\layout\fragment_collection_object.xml to you project\res\layout. This,I guess will solve the problem.

Android : refresh fragment with dynamic list using ViewPager and ActionbarTab

I'm quite new to Android. I'm stuck with this issue for a few days now.
I'm using the new google's Template of Action bar Tab with swipe (ViewPager).
Inside each fragment, I want to display a dynamic list of cards (with cardslib : https://github.com/gabrielemariotti/cardslib). The data for the cards come from an asyncTask which is fetching data from an url.
This is my problem : When I launch the app, the first View is display, but the ViewPager prepare the view for the second page, so the async task for the second screen is launch too. And the cards of the second screen are added to the first one.
Then if i swipe to the second screen, nothing is displayed, and the async task for the third screen is launch. So if swipe to the thrid screen, nothing... And then if i switch again to the second screen, the cards with data of the first screen are displayed... A real mess !
I think I have a probleme with the onCreate, onCreateView, or something like that. Maybe the code is sometimes not at the right place.. I don't know. I tried a lot of small modofication, but nothing worked. (but it worked like this with just action bar tab and without the ViewPager).
Thanks
Here is my code :
import java.util.Locale;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
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 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.v13.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 activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mSectionsPagerAdapter.notifyDataSetChanged();
// 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);
//mSectionsPagerAdapter.notifyDataSetChanged();
}
});
// 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));
}
mSectionsPagerAdapter.notifyDataSetChanged();
}
#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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
if(tab.getPosition() != mViewPager.getCurrentItem()) {
mViewPager.setCurrentItem(tab.getPosition());
}
// mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
mSectionsPagerAdapter.notifyDataSetChanged();
}
#Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
mSectionsPagerAdapter.notifyDataSetChanged();
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#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;
}
}
}
And here is the Fragment
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardArrayAdapter;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.view.CardListView;
import java.util.ArrayList;
import java.util.List;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.example.webanajones.asyncTask.GetItemTask;
import com.example.webanajones.asyncTask.IResultsListener;
import com.example.webanajones.asyncTaskResponse.ItemJsonResponse;
import com.example.webanajones.asyncTaskResponse.ItemJsonResponse.Item;
/**
* A placeholder fragment containing a simple view.
*/
public class PlaceholderFragment extends Fragment implements
IResultsListener {
/**
* The fragment argument representing the section number for this
* fragment.
*/
static final String ARG_SECTION_NUMBER = "section_number";
public static int limit = 0;
IResultsListener listener = this;
ArrayList<Card> cards = new ArrayList<Card>();
CardArrayAdapter mCardArrayAdapter;
int flag = 0;
boolean loadMore = true;
CardListView listView;
private int idx;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Bundle data = getArguments();
// idx = data.getInt("idx") +1;
limit = 0;
flag = 0;
loadMore = true;
List<String> params = new ArrayList();
params.add(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
params.add(String.valueOf(limit));
GetItemTask getItemTask = new GetItemTask();
getItemTask.setOnResultsListener(listener);
getItemTask.execute(params);
}
/**
* Returns a new instance of this fragment for the given section number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#SuppressWarnings("unchecked")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
#Override
public void onGetItemTaskSucceeded(ItemJsonResponse itemJsonResponse) {
if (mCardArrayAdapter != null) {
mCardArrayAdapter.notifyDataSetChanged();
}
if (itemJsonResponse.getSuccess() != 0) {
loadMore = true;
final Item[] itemTable = itemJsonResponse.getItemArray();
for (int itemIndex = 0; itemIndex < itemTable.length; itemIndex++) {
if (itemTable[itemIndex].getTypeId() == 2) { // 2 = type GIF
final int index = itemIndex;
Card gifCard = new CardGif(getActivity()){
#Override
public void setupInnerViewElements(ViewGroup parent, View view) {
DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
int width = (int) (metrics.widthPixels * 0.97);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.WRAP_CONTENT);
params.bottomMargin = 30;
LinearLayout LinearLayout = (LinearLayout) parent.findViewById(R.id.inner_frame);
GIFWebView gifView = new GIFWebView (getActivity(), itemTable[index].getObjectUrl());
LinearLayout.addView(gifView);
}
};
CardHeader header = new CardHeader(getActivity());
header.setTitle(itemTable[itemIndex].getTitle());
cards.add(gifCard);
}
if (itemTable[itemIndex].getTypeId() == 1) { // 1 = image
Card card = new Card(getActivity());
CardHeader header = new CardHeader(getActivity());
header.setTitle(itemTable[itemIndex].getTitle());
card.addCardHeader(header);
MyThumbnail thumb = new MyThumbnail(getActivity(), itemTable[itemIndex].getObjectUrl());
thumb.setExternalUsage(true);
card.addCardThumbnail(thumb);
final int index = itemIndex;
card.addPartialOnClickListener(Card.CLICK_LISTENER_THUMBNAIL_VIEW, new Card.OnCardClickListener() {
#Override
public void onClick(Card card, View view) {
Intent intent = new Intent(getActivity(), FullscreenActivity.class);
intent.putExtra("imageUrl", itemTable[index].getObjectUrl());
startActivity(intent);
}
});
cards.add(card);
}
}
if (flag == 0) {
mCardArrayAdapter = new CardArrayAdapter(getActivity(), cards);
listView = (CardListView) getActivity().findViewById(
R.id.carddemo_list);
listView.setAdapter(mCardArrayAdapter);
flag = 1;
}
}
else {
loadMore = false;
}
loadMoreCards(mCardArrayAdapter, loadMore);
}
public void loadMoreCards(CardArrayAdapter mCardArrayAdapter, boolean loadMore) {
if (listView != null) {
mCardArrayAdapter.notifyDataSetChanged();
if (loadMore) {
listView.setOnScrollListener(new EndlessScrollListener() {
#Override
public void onLoadMore(int page, int totalItemsCount) {
limit += 5;
List<String> params = new ArrayList();
params.add("1");
params.add(String.valueOf(limit));
GetItemTask getItemTask = new GetItemTask();
getItemTask.setOnResultsListener(listener);
getItemTask.execute(params);
}
});
}
}
}
//fin placeholderfrag
#Override
public void onGetImageTaskSucceeded(Bitmap bitmap) {
// TODO Auto-generated method stub
}
}
Thanks #GabrieleMariotti, it helped ! In fact I already tried to use different classes for the different fragments, but I forget to give diferent IDs to my cardsLists. It solved the problem. Btw, good job for your lib, it is awesome.
So for the next one : create differents classes for all your differents fragments. They have to use differents layout, and the cardsList have to use differents ID.

Android Swipe tabs Issue: Can't seem to update the UI and the result comes repeated n times

I'm using two classes. One for the background service where i'm doing several of gatherings of information, and the other I decided to use a swipe tab view to manage and organize my information/data. In the background service (like I said) i'm retrieving information (mostly string types) and sending them via intent to the Main Activity, this activity receives it via a broadcastreceiver (this part works fine). Now the problem i'm having is that when i try to distribute the information the the diferent tabs, it comes repeated and doesn't seem to refresh now my most crucial problem is the reapeating information part. Going to place my code and see if any one of you brilliant minds :D can help me. I hope you can.
import java.util.Locale;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
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.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ActivityMain extends FragmentActivity implements
ActionBar.TabListener {
int NUM_TAB = 3;
static TextView InfoView1;
static TextView InfoView2;
static TextView InfoView3;
static String infoData1 = "";
static String infoData2 = "";
static String infoData3 = "";
Fragment frag;
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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));
}
Defining the intent
intent = new Intent(this, BroadcastService.class);
}
Starting the broadcastreceiver to call the Background service
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
};
#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;
}
#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.
switch (position)
{
case 0:
frag = new Information1();
Bundle arg0 = new Bundle();
arg0.putString(Information1.ARG_SECTION, infoData1);
frag.setArguments(arg0);
break;
case 1:
frag = new Information2();
Bundle arg1 = new Bundle();
arg1.putString(Information2.ARG_SECTION, infoData2);
frag.setArguments(arg1);
break;
case 2:
frag = new Information3();
Bundle arg2 = new Bundle();
arg2.putString(BatteryStatus.ARG_SECTION, infoData3);
frag.Information3(arg2);
break;
default:
frag = null;
break;
}
return frag;
}
#Override
public int getCount() {
// Show 3 total pages.
return NUM_TAB;
}
#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;
}
}
private void updateUI(Intent intent) {
String stuff1 = intent.getStringExtra("stuff");
String stuff2 = intent.getStringExtra("stuff2");
String stuff3 = intent.getStringExtra("stuff3");
String stuff4 = intent.getStringExtra("stuff4");
String stuff5 = intent.getStringExtra("stuff5");
String stuff6 = intent.getStringExtra("stuff6");
so on...
And storing them in a string
infoData1 += ("Stuff1: " + stuff1 + "\n");
infoData1 += ("Stuff2: " + stuff2 + "\n");
infoData2 += ("Stuff3: " + Stuff3 + "\n");
infoData2 += ("Stuff4: " + Stuff4 + "\n");
infoData3 += ("Stuff5: " + Stuff5 + "\n");
infoData3 += ("Stuff6: " + Stuff6 + "\n");
}
public static class Information1 extends Fragment {
public static final String ARG_SECTION = "section_number";
public Information1(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View info1View = inflater.inflate(
R.layout.information1, container, false);
InfoView1 = (TextView) info1View
.findViewById(R.id.info1);
InfoView1.setText(getArguments().getString(ARG_SECTION));
Log.d("InfoView1", infoData1);
return info1View;
}
}
public static class Information2 extends Fragment {
public static final String ARG_SECTION = "section_number";
public Information2(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View info2View = inflater.inflate(
R.layout.Information2, container, false);
InfoView2 = (TextView) info2View
.findViewById(R.id.info2);
InfoView2.setText(getArguments().getString(ARG_SECTION));
Log.d("InfoView2", infoData2);
return info2View;
}
}
public static class Information3 extends Fragment {
public static final String ARG_SECTION = "section_number";
public Information3() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View info3View = inflater.inflate(
R.layout.Information3, container, false);
InfoView3 = (TextView) info3View
.findViewById(R.id.info3);
InfoView3.setText(getArguments().getString(ARG_SECTION));
Log.d("BatteryStatView", infoData3);
return info3View;
}
}
#Override
public void onResume() {
super.onResume();
startService(intent);
registerReceiver(broadcastReceiver, new IntentFilter(BroadcastService.BROADCAST_ACTION));
}
#Override
public void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
stopService(intent);
}
}
Any ideas?! =D

Categories

Resources