Pass String to a fragment from an Activity - android

I need to pass some values to the fragment from an Activity class.
I tried passing the bundle to the fragment but got null.
I need to receive the values and then run an AsyncTask in the fragment activity.
Code snippet FirstFragment.java:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.first_frag, container, false);
Bundle bundle = getArguments();
if(bundle!=null)
{
sensorUID=getArguments().getString("senID");
System.out.println("we got this: " + sensorUID);
}
System.out.println("we got this2: "+ sensorUID);
new MyAsyncTask().execute();
return v;
}
Code (activity class):
package test.sensorv2;
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;
public class scatter extends FragmentActivity {
protected String sensorUID;
protected String tankname;
protected String SCALE_M;
protected String SCALE_C;
protected String DPs;
protected String UNITs;
String np;
int k = 0;
float m;
float c;
int numVar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scatter);
sensorUID = getIntent().getStringExtra("sUID");
tankname = getIntent().getStringExtra("tNAME");
SCALE_C = (getIntent().getStringExtra("tSCALEC"));
SCALE_M = (getIntent().getStringExtra("tSCALEM"));
DPs = (getIntent().getStringExtra("tDP"));
UNITs = (getIntent().getStringExtra("tUNIT"));
numVar = Integer.parseInt(DPs);
Bundle b=new Bundle();
b.putString("key", sensorUID);
FirstFragment f=new FirstFragment();
f.setArguments(b);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return FirstFragment.newInstance("FirstFragment, Instance 1");
case 1:
return SecondFragment.newInstance("SecondFragment, Instance 1");
default:
return ThirdFragment.newInstance("ThirdFragment, Default");
}
}
#Override
public int getCount() {
return 3;
}
}
}

#Override
public Fragment getItem(int pos) {
Fragment fragment;
Bundle args = new Bundle();
args.putString("sendID", sensorUID);
switch (pos) {
case 0:
fragment = FirstFragment.newInstance("FirstFragment, Instance 1");
break;
case 1:
fragment = SecondFragment.newInstance("SecondFragment, Instance 1");
break;
default:
fragment = ThirdFragment.newInstance("ThirdFragment, Default");
}
fragment.setArguments(args);
return fragment;
}
Then you can retrieve the arguments in your fragment using `getArguments.getString("sendID");
Or you can just pass your ID as a parameter in newInstance like this fragment = FirstFragment.newInstance(senId); and in new Instance you pass the data from newInstance to onCreateview like this :
public static Fragment newInstance(String senID){
FirstFragmentf = new FirstFragment();
Bundle args = new Bundle();
args.putString("senID", sendID);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorID= getArguments().getString("sendID");
}

Create a field in the fragment:
public Bundle bundle;
and in the mainactivity assign your bundle to
f.bundle=b;
and delete
Bundle bundle=getArguments();

In your activity put:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Actiity to Fragment
Bundle bundle = new Bundle();
bundle.putString("test", "From Activity");
// set Fragmentclass Arguments
TestFragment testFragment = new TestFragment();
testFragment.setArguments(bundle);
testFragment.myTest();
}
In your Fragment put:
public void myTest(){
//Get String from Activity
String stringText = getArguments().getString("test");
Log.d(LOG_TAG, "The String from the Activity = " + stringText);
}

Related

How to send data from Activity to Fragment with ViewPager2?

I'm trying to send data from MainActivity to one of the fragments which was created using ViewPager2.
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ViewPager2 viewPager = findViewById(R.id.viewPager);
MainPagerAdapter pagerAdapter = new MainPagerAdapter(this);
viewPager.setAdapter(pagerAdapter);
}
MainPagerAdapter:
public class MainPagerAdapter extends FragmentStateAdapter
{
public MainPagerAdapter(#NonNull FragmentActivity fragmentActivity)
{
super(fragmentActivity);
}
#NonNull
#Override
public Fragment createFragment(int position)
{
switch (position)
{
case 0:
return new Fragment1();
case 1:
return new Fragment2();
default:
return new Fragment3();
}
}
#Override
public int getItemCount()
{
return 3;
}
}
I send data in MainActivity:
Fragment1 fragment1 = new Fragment1();
Bundle bundle = new Bundle();
bundle.putInt("1", 1);
fragment1.setArguments(bundle);
And try to get it in Fragment1:
Bundle bundle = this.getArguments();
if (bundle != null)
{
int i = bundle.getInt("1", 0);
Log.d(Constants.LOG_TAG, "" + i);
}
But it always comes with an empty bundle because the fragment is created when the viewpager scrolls through it.
How can I send data to Fragment when MainActivity is created?
My solution:
MainActivity:
pagerAdapter = new MainPagerAdapter(this);
pagerAdapter.setData(fragment1Container);
MainPagerAdapter:
public class MainPagerAdapter extends FragmentStateAdapter
{
private Fragment1Container fragment1Container;
private Fragment2Container fragment2Container;
public void setData(Fragment1Container container)
{
fragment1Container = container;
}
public void setData(Fragment2Container container)
{
fragment2Container = container;
}
#NonNull
#Override
public Fragment createFragment(int position)
{
switch (position)
{
case 0:
Fragment1 fragment1 = new Fragment1();
Bundle bundle = new Bundle();
Gson gson = new Gson();
bundle.putString("fragment1Container", gson.toJson(fragment1Container));
fragment1.setArguments(bundle);
return fragment1;
case 1:...
Fragment1:
private Fragment1Container fragment1Container;
if (bundle != null)
{
Gson gson = new Gson();
String jsonString = bundle.getString("fragment1Container");
Type entityType = new TypeToken<Fragment1Container>(){}.getType();
fragment1Container = gson.fromJson(jsonString , entityType );
}

Passing data from recyclerview adapter of activity-1 to viewpager fragment of activity-2

my recyclerView Adapter - Activity-1
here i'm sending arguments to ViewPager Fragmnet using setArguments()
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putString("projectName", jobCardLists.get(position).getProject());
// set MyFragment Arguments
DetailsFragment detailsFragment = new DetailsFragment();
detailsFragment.setArguments(bundle);
Intent intent = new Intent(context, JobCardViewActivity.class);
context.startActivity(intent);
});
}
Activity-2 which contains ViewPager - Tab layout
public class JobCardViewActivity extends AppCompatActivity {
private ViewPager mViewPager;
private Toolbar mToolbar;
private JobCardViewVPAdapter mViewPagerAdapter;
private TabLayout mTabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_job_card_view);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
//set TabLayout
setViewPager();
}
private void setViewPager() {
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPagerAdapter = new JobCardViewVPAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mViewPagerAdapter);
mTabLayout = (TabLayout) findViewById(R.id.tab);
mTabLayout.setupWithViewPager(mViewPager);
mTabLayout.getTabAt(0).setIcon(R.drawable.ic_home_white_24dp);
}
}
My viewPager Adapter
public class JobCardViewVPAdapter extends FragmentStatePagerAdapter {
private static int TAB_COUNT = 6;
public JobCardViewVPAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return DetailsFragment.newInstance();
case 1:
return WorkActivitiesFragment.newInstance();
case 2:
return DocumentsFragment.newInstance();
case 3:
return TasksFragment.newInstance();
case 4:
return WorkImagesFragment.newInstance();
case 5:
return NotesFragment.newInstance();
}
return null;
}
#Override
public int getCount() {
return TAB_COUNT;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 1:
return WorkActivitiesFragment.TITLE;
case 2:
return DocumentsFragment.TITLE;
case 3:
return TasksFragment.TITLE;
case 4:
return WorkImagesFragment.TITLE;
case 5:
return NotesFragment.TITLE;
}
return super.getPageTitle(position);
}
}
My ViewPager fragment class
here i'm getting NULL from getArguments()
public class DetailsFragment extends Fragment {
private View view;
private Activity activity;
public static final String TITLE = "Details";
private String projectName;
public DetailsFragment() {
// Required empty public constructor
}
public static DetailsFragment newInstance() {
return new DetailsFragment();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
if (getArguments() != null) {
Bundle bundle = getArguments();
projectName = bundle.getString("projectName");
} else {
Toast.makeText(activity, "NULL", Toast.LENGTH_SHORT).show();
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_details, container, false);
TextView tvProjectName, startDate, endDate, assignedBy, workCategory, status;
tvProjectName = view.findViewById(R.id.tv_project_name);
tvProjectName.setText(projectName);
return view;
}
1.You can use Single Object (singleton) for your fragment.
In your fragment use,
private static DetailsFragment ourInstance = new DetailsFragment();
public static DetailsFragment newInstance() {
return ourInstance;
}
And in your onBindViewHolder(), use
DetailsFragment detailsFragment = DetailsFragment.newInstance();
detailsFragment.setArguments(bundle);
2.Or else, pass data between the activities
In your onBindViewHolder(), use
Intent intent = new Intent(context, JobCardViewActivity.class);
intent.putString("project", jobCardLists.get(position).getProject());
context.startActivity(intent);
and do,
1.in JobCardViewActivity,get the string and save it
myString=getIntent().getString("project"); //public String myString;
2.in fragment, use that saved string
tvProjectName.setText(((JobCardViewActivity)getActivity()).myString);

How to parse JSON data into fragments?

Hello friends im developing android app with fragments in a viewpager but now i want to parse JSON data into the fragment.my question is how can i set number of fragments as the size of json data and insert the json data into the required fragment.please help me ive been searching for it on internet for hours but disappointment results so please help me.Thanks in advance!.
[{"c_id":13,"category_name":"PUBLICATIONS","imagename":"http://goringr.com/church_project/churchimagesnew/publications.jpg","ctype":"church"},{"c_id":14,"category_name":"YOUTH ASSOCIATION","imagename":"http://goringr.com/church_project/churchimagesnew/youthassociation.jpg","ctype":"church"}]
this is my json data and all i want is to display contents of object with c_id=13 in first fragment and display contents of object with c_id=14 in second fragment.
MainActivity.java
package project1.test;
import android.content.Intent;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public static final String data = "hello";
FragmentPagerAdapter adapterViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Toolbar tool = (Toolbar)findViewById(R.id.tool);
// setSupportActionBar(tool);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager);
adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
vpPager.setAdapter(adapterViewPager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.sample_actions, menu);
return true;
}
public static class MyPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 3;
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Returns total number of pages
#Override
public int getCount() {
return NUM_ITEMS;
}
// Returns the fragment to display for that page
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return FirstFragment.newInstance(0, "Fragment1");
case 1:
return FirstFragment.newInstance(1, "Fragment2");
case 2:
return SecondFragment.newInstance(2, "Fragment3");
default:
return null;
}
}
// Returns the page title for the top indicator
#Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
}
}
FirstFragment
public class FirstFragment extends Fragment {
// Store instance variables
private String title;
private int page;
// newInstance constructor for creating fragment with arguments
public static FirstFragment newInstance(int page, String title) {
FirstFragment fragmentFirst = new FirstFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
// Store instance variables based on arguments passed
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
page = getArguments().getInt("someInt", 0);
title = getArguments().getString("someTitle");
}
// Inflate the view for the fragment based on layout XML
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
;
View view = inflater.inflate(R.layout.fragment_first, container, false);
// TextView tvLabel = (TextView) view.findViewById(R.id.tvLabel);
//// tvLabel.setText(page + " -- " + title);
CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout)view.findViewById(R.id.collapsing_toolbar1);
collapsingToolbarLayout.setTitle("black panther");
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.sample_actions, menu);
}
}
SecondFragment
public class SecondFragment extends Fragment {
// Store instance variables
private String title;
private int page;
Button tryit;
// newInstance constructor for creating fragment with arguments
public static SecondFragment newInstance(int page, String title) {
SecondFragment fragmentSecond = new SecondFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentSecond.setArguments(args);
return fragmentSecond;
}
// Store instance variables based on arguments passed
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page = getArguments().getInt("someInt", 0);
title = getArguments().getString("someTitle");
}
// Inflate the view for the fragment based on layout XML
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_second, container,
false);
tryit = (Button) view.findViewById(R.id.tryit);
tryit.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),ManagingProfile.class);
startActivity(intent);
}
});
// TextView tvLabel = (TextView) view.findViewById(R.id.tvLabel);
// tvLabel.setText(page + " -- " + title);
return view;
}
}
you can parse like this:
List<RidesData> ridesDatas; /* this is should be parcelable */
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(FragmentAbout.newInstance(ridesDatas), "ABOUT");
adapter.addFragment(FragmentPointOfInterest.newInstance(ridesDatas),"POI");
viewPager.setAdapter(adapter);
}
and set constructure to each fragment like this:
FragmentAbout.class
public static FragmentAbout newInstance(List<RidesData> ridesDatas) {
FragmentAbout fragmentAbout = new FragmentAbout();
Bundle arg = new Bundle();
arg.putParcelableArrayList("data", (ArrayList<? extends Parcelable>) ridesDatas);
fragmentAbout.setArguments(arg);
return fragmentAbout;
}
FragmentPointOfInterest.class
public static FragmentPointOfInterest newInstance(List<RidesData> ridesDatas) {
FragmentPointOfInterest fragmentPointOfInterest = new FragmentPointOfInterest();
Bundle arg = new Bundle();
arg.putParcelableArrayList("data", (ArrayList<? extends Parcelable>) ridesDatas);
fragmentPointOfInterest.setArguments(arg);
return fragmentPointOfInterest;
}
and get each data of json in onViewCreated() method of fragment like this:
you have 2 position of data in json.
[0] = c_id=13
[1] = c_id=14
so in first fragment set 0 position:
RidesData data = ridesData.get(0);
and in second Fragment set 1 postion:
RidesData data = ridesData.get(1);
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ridesData = new ArrayList<>();
ridesData = getArguments().getParcelableArrayList("data");
RidesData data = ridesData.get(1);
}
You have to make use of custom adapter to extract the json data and view holder to print the data.
refer this link

Android Fragment in the wrong place

I have a weird problem.
In the main activity I use this code:
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = {"Laatste Nieuws", "Uitrukken", "Voertuigen", "Contact", "Top Grossing"};
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
#Override
public int getCount() {
return TITLES.length;
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = NaviContentFragment.newInstance(position);
break;
case 1:
fragment = NaviContentFragmentTwo.newInstance(position);
break;
case 2:
fragment = NaviContentFragment.newInstance(position);
break;
case 3:
fragment = NaviContentFragment.newInstance(position);
break;
case 4:
fragment = NaviContentFragment.newInstance(position);
break;
default:
break;
}
return fragment;
}
}
So when I open a tab it will open a new fragment. And when I open tab 1 labeled "Uitrukken" it should open a other fragment than the other ones. This fragment:
public class NaviContentFragmentTwo extends Fragment {
private static final String ARG_POSITION = "position";
public static NaviContentFragment newInstance(int position) {
NaviContentFragment fragment = new NaviContentFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
fragment.setArguments(b);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = rootView = inflater.inflate(R.layout.navi_content_two, container, false);
TextView textViewNaviConTwo = (TextView) rootView.findViewById(R.id.textViewNaviConTwo);
textViewNaviConTwo.setText("HALLLOOOTJES :)");
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
With the Text "HALLLOOOTJES :)". And only in that fragment. But the other way around happens. This text is show on all the tabs except for tab number 1.
I'm certainly missing something.
Screenshots:
It should say "HALLLOOOTJES :)"
In your NaviContentFragmentTwo class, you are returning a NaviContentFragment from your newInstance method.
My guess is you should be returning a NaviContentFragmentTwo instance.
So it should be something like..
public class NaviContentFragmentTwo extends Fragment {
private static final String ARG_POSITION = "position";
public static NaviContentFragmentTwo newInstance(int position) {
NaviContentFragmentTwo fragment = new NaviContentFragmentTwo ();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
fragment.setArguments(b);
return fragment;
}
This code could be the possible cause:
public static NaviContentFragment newInstance(int position) {
NaviContentFragment fragment = new NaviContentFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
fragment.setArguments(b);
return fragment;
}
Try replacing NaviContentFragment to NaviContentFragmentTwo.

When to update information for ViewPager?

If I ave an activity with a viewPAger on it and two sets of data that I want to display, is it best to have one fragment and load information into textViews based on position passed from activity or better to create more than one fragment and load the one needed?
Any help is greatly appreciated.
public class testFragment extends Fragment {
private static final String ARG_PAGE = "pageNumber";
private static final int DEFAULT_INT = 0;
TextView header, sd1;
int h1, page = 0;
public testFragment (){
}
public testFragment newInstance(int pageNumber) {
testFragment fragment = new testFragment ();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
page = getArguments().getInt(ARG_PAGE);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_high_scores_page, container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
header = (TextView) getActivity().findViewById(R.id.modes_header);
sd1 = (TextView) getActivity().findViewById(R.id.stat_det_1);
switch (page) {
case 1:
header.setText(R.string.easy_mode);
h1 = DEFAULT_INT;
sh1 = DEFAULT_INT;
sd1.setText(String.valueOf(h1));
break;
case 2:
header.setText(R.string.medium_mode);
h1 = DEFAULT_INT + 1;
sh1 = DEFAULT_INT + 1;
sd1.setText(String.valueOf(h1));
break;
default:
header.setText(R.string.action_restart);
h1 = DEFAULT_INT;
sh1 = DEFAULT_INT;
sd1.setText(String.valueOf(h1));
break;
}
}
public void setTextViewInfo(){
sd1.setText(String.valueOf(h1));
}
}
Or would it be better to send the information from my activity and load one or multiple fragments instead?
public class testActivity extends FragmentActivity {
private static final int NUM_PAGES = 8;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testActivity);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.high_scores_pager);
mPagerAdapter = new testActivity (getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class testActivityAdapter extends FragmentStatePagerAdapter {
public testActivityAdapter (FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
testFragment tf1 = new testFragment ();
notifyDataSetChanged();
return easy.newInstance(position);
case 1:
testFragment2 tf2 = new testFragment ();
notifyDataSetChanged();
return medium.newInstance(position);
default:
testFragment d = new testFragment ();
return d.newInstance();
}
}
#Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
edit:
In my adapter, I just tried
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return HighScoresEasyFragment.newInstance(position);
case 1:
return HighScoresMediumFragment.newInstance(position);
case 2:
return HighScoresExpertFragment.newInstance(position);
case 3:
return HighScoresBrainFartFragment.newInstance(position);
case 4:
return HighScoresClassicFragment.newInstance(position);
case 5:
return HighScoresPuzzleFragment.newInstance(position);
case 6:
return HighScoresPerfectFragment.newInstance(position);
case 7:
return HighScoresPowerFragment.newInstance(position);
default:
return HighScoresEasyFragment.newInstance(position);
}
ugh, but it didn't load anything... my constructor is now...:
public static HighScoresEasyFragment newInstance(int pageNumber) {
HighScoresEasyFragment fragment = new HighScoresEasyFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
It loaded the first page's header but no data and it didn't load anything for second to 8th fragments...
You should call notifyDataChanged() from your activity using your adapter reference. Basically notifyDataChanged just tells all your fragments in your adapter that data has changed and they should refresh themselves. Remove notifyDataChanged from your getItem() method and put it when you change data in your activity.

Categories

Resources