I have a listview containing item apple, banana, orange ...in second screen activity
when i click on particular item on apple it navigates to Details screen
here output has to appear like apple ..if i swipe page then banana and for nextswipe orage
apple-->banana-->orange
but am getting output like apple-->apple-->apple.
Intent detailIntent =new Intent(SecondScreen.this, Details.class);
startActivity(detailIntent);
public class MyApplication extends Application {
ArrayList<String> totalvalue = new ArrayList<String>();
}
public class Details extends FragmentActivity{
MyApplication app;
ViewPager pager;
MyFragmentPagerAdapter pagerAdapter;
public static int position ;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.detail);
app = ((MyApplication) getApplicationContext());
pager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
pagerAdapter = new MyFragmentPagerAdapter(fm,app.totalvalue);
pager.setAdapter(pagerAdapter);
}
public static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private static ArrayList<String> temperList;
public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<String> totalvalue ) {
super(fm);
this.temperList = totalvalue;
}
public Fragment getItem(int position) {
return ThingFragment.newInstance((temperList.get(position)));
}
#Override
public int getCount(){
return temperList.size();
}
private static class ThingFragment extends Fragment {
private String name1;
static ThingFragment newInstance(String string ) {
ThingFragment f = new ThingFragment();
Bundle args = new Bundle();
args.putString("name",temperList.get(position) );
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
name1 = getArguments().getString("name");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
TextView t = (TextView)v.findViewById(R.id.prodcutt);
t.setText(""+ name1);
return v;
}
}
}
}
args.putString("name",temperList.get(position) ); shouldn't be possible inside your ThingFragment as its static.
That should be args.putString("name",string);
Also your temperList shouldn't be static in your adapter (no reason to, your passing it as a param) just make it final - private final ArrayList<String> temperList;
If that doesn't fix it please post more structured separated code so we can see how your application is built up a bit more. The adapter looks fine, so it maybe a case that your writing/overwriting the array at some point.
Related
I'm creating an app with vertical page adapter using FragmentStatePagerAdapter. The big issue i'm facing is, data is not displayed on the textview on first app launch but is displayed on scrolling the page. I believe the fragment view is delaying to create textview because, on my LoadAlbumDataCompleted() function inside Fragmentone.class, i'm able to print the data returned or also output via toast but is not getting populated to the textview.
Kindly help.
MainActivity.class
public class MainActivity extends FragmentActivity implements LoadAalbumsTotalListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new MainActivityVSlideAdapter(this, getSupportFragmentManager(), NUMBER_OF_PAGES);
mPager.setAdapter(mAdapter);
LoadTotalAlbumsNum.BindAlbumsTotalListener(this);
}
#Override
public void OnLoadAlbumsCompleted(String total) {
if(total.trim().equalsIgnoreCase("")){
NUMBER_OF_PAGES=0;
}else{
NUMBER_OF_PAGES=Integer.parseInt(total.trim());
}
mAdapter = new MainActivityVSlideAdapter(this, getSupportFragmentManager(), NUMBER_OF_PAGES);
mPager.setAdapter(mAdapter);
}
}
MainActivityVSlideAdapter.class Adapter
public class MainActivityVSlideAdapter extends FragmentStatePagerAdapter {
static int NUMBER_OF_PAGES;
private Context con;
public MainActivityVSlideAdapter(Context con, FragmentManager fm, int NUMBER_OF_PAGES) {
super(fm);
this.con=con;
this.NUMBER_OF_PAGES=NUMBER_OF_PAGES;
}
#Override
public int getCount() {
return NUMBER_OF_PAGES;
}
#Override
public Fragment getItem(int position) {
return FragmentOne.newInstance(position);
}
}
Fragmentone.class
public class FragmentOne extends Fragment implements LoadAlbumDataListener {
private static final String MY_NUM_KEY = "num";
private int mNum;
private TextView SaloonName;
private TextView location;
// You can modify the parameters to pass in whatever you want
public static FragmentOne newInstance(int num) {
FragmentOne f = new FragmentOne();
Bundle args = new Bundle();
args.putInt(MY_NUM_KEY, num);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get argument from
mNum = getArguments() != null ? getArguments().getInt(MY_NUM_KEY) : 0;
session=new Session(getActivity());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one, container, false);
Methods methods=new Methods(getActivity());
// v.setBackgroundColor(mColor);
SaloonName = v.findViewById(R.id.SaloonName);
location=v.findViewById(R.id.location);
new LoadAlbumData(getActivity()).execute(getString(R.string.urlAddress)+"load-album-data.php", String.valueOf(mNum));
LoadAlbumData.BindLoadAlbumDataListener(this);
return v;
}
#Override
public void LoadAlbumDataCompleted(String s) {
JSONArray jsonPicsArray = null;
JSONObject jsonObj;
String BusinessLocation=null;
try {
jsonPicsArray = new JSONArray(s);
businessName = jsonObj.getString("businessName");
BusinessLocation = jsonObj.getString("location");;
}
Toast.makeText(getActivity(), businessName, Toast.LENGTH_LONG).show();
SaloonName.setText(businessName);
location.setText(BusinessLocation);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
From your code, it looks like, you are not setting the LoadAlbumDataListener correctly. Instead of statically setting the listener. Pass reference in constructor of your LoadAlbumData Asynctask
remove this line
LoadAlbumData.BindLoadAlbumDataListener(this);
replace
new LoadAlbumData(getActivity()).execute(getString(R.string.urlAddress)+"load-album-data.php", String.valueOf(mNum));
with
new LoadAlbumData(getActivity(), this).execute(getString(R.string.urlAddress)+"load-album-data.php", String.valueOf(mNum));
Modify your asynctask to have reference of LoadAlbumDataListener
Also as a good practice, never store strong reference of activity or fragment in your asynctask. Use WeakReference.
I am trying to get some text from editTexts on different fragments. So what I do first is define my mPager and mPagerAdapter:
a_Atenuacion Activity
public class a_Atenuacion extends FragmentActivity{
private static final int NUM_PAGES = 3;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_dat_viewpager);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0:
return new a_Dat_Inicio1();
case 1:
return new a_Dat_Inicio2();
case 2:
return new a_Dat_Inicio3();
}
return null;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
Then I get my 3 fragments classes (Both 1st and 2nd layouts have an editText, but the 3rd one has an editText and a button). The button function is that when I am in the last fragment (fragment3) it take info form (different editTexts) and send to another activity.
a_Dat_Inicio1 Fragment
public class a_Dat_Inicio1 extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.a_dat_inicio1, container, false);
return view;
}
}
a_Dat_Inicio3 Fragment
public class a_Dat_Inicio3 extends Fragment {
EditText edit3;
EditText edit2;
EditText edit1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.a_dat_inicio3, container, false);
edit1 = (EditText)getActivity().findViewById(R.id.editText1);
final String edit11 = edit1.getText().toString();
edit2 = (EditText)getActivity().findViewById(R.id.editText2);
final String edit22 = edit2.getText().toString();
edit3 = (EditText)view.findViewById(R.id.editText3);
final String edit33 = edit3.getText().toString();
Button but=(Button) view.findViewById(R.id.button);
but.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//Creamos el bundle
Bundle bundle = new Bundle();
//Le ponemos la variable parametro con el contenido (key, valor)
bundle.putString("edit3", edit33);
bundle.putString("edit2", edit22);
bundle.putString("edit1", edit11);
Intent net= new Intent(v.getContext(),Prueba1.class);
net.putExtras(bundle);
startActivity(net);
}
});
return view;
}
}
Finally I get bundle on another activity (Prueba1.class) and it is curious that I only get result for editText1 (on 1st fragment) and rest are null.
Can anybody give me a help?
Thanks in advance.
finally I get over an interface, that is the only way I think to get soemthing on already defined fragments.
My code get like this:
1st define an interface
public interface OnEditTextChanged {
public void onEditPressed1(String edit1);
public void onEditPressed2(String edit1);
public void onEditPressed3(String edit1);
Then fragment activity:
public class a_Dat_Inicio1 extends Fragment {
EditText edit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.a_1dat_inicio1, container, false);
init (view);
return view;
}
OnEditTextChanged editListener;
#Override
public void onAttach(Activity activity){
super.onAttach(activity);
try{
editListener=(OnEditTextChanged) getActivity();
}catch(ClassCastException e){
throw new ClassCastException(activity.toString()+"must implemnt onEditPressed");
}
}
private void init(View view) {
edit=(EditText) view.findViewById(R.id.editText1);
//cada vez que se modifique texto llamar
edit.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
final String edit11 = edit.getText().toString();
editListener.onEditPressed1(edit11);
}
});
And finally on our main activity, call the method:
public class a_Atenuacion extends FragmentActivity implements OnEditTextChanged {
String dat1;
String dat2;
String dat3;
private static final int NUM_PAGES = 3;
private PagerAdapter mPagerAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_1dat_viewpager);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
//HERE DO WHATEVER YOU WANT WITH THE DATA CAUGTH ON THE EDIT 1 METHOD
}
#Override
public void onEditPressed1(String edit1) {
if(mPager.getCurrentItem()==0){
dat1=edit1;
Toast bread = Toast.makeText(getApplicationContext(), "edit1", Toast.LENGTH_LONG);
bread.show();
}
}
HOPE this help someone!!!
In any way thanks!!!
Try this:
In your second fragment, pass the intent extras and your value. For example:
In you second fragment, do this :
Intent someintent = new Intent();
//Some first result
someintent.putExtra("your_value_one", your_value_one);
//Some Second result
someintent.putExtra("your_value_two", your_value_two);
//Some third result
someintent.putExtra("your_value_three", your_value_three);
getActivity().setResult(getActivity().RESULT_OK,someintent);
getActivity().finish();
In your other fragment, where you want this result, do this on your other fragment like this:
1) Make some method to get the info.
private void getInfo() {
//Note: The values are coming from a diff activity or fragment and so the strings should match.
Intent data = new Intent();
String first_value = data.getStringExtra("your_value_one");
String second_value = data.getStringExtra("your_value_two");
String third_value = data.getStringExtra("your_value_three");
Log.i("First Value: ", first_value);
Log.i("First Value: ", second_value);
Log.i("First Value: ", third_value);
}
After making this method, just call it on your onActivityCreated().
Now you can use those string however and wherever you want to. If you want to use those values anywhere else, make sure to define you strings at the very start so that you can use the values anywhere.
Hope this answer helps .. :)
I am going through the Android Developer guide on View Paging with fragments and am stuck at populating the fragment from my main onCreate method. I think I am confused as to what methods get called when to populate the fragment as I am getting null values.
My Fragment Class
public class CatalogFragment extends Fragment {
private String filename;
private String author;
private String title;
public CatalogFragment(String f, String t, String a){
filename = f;
author = a;
title = t;
Log.d("frag", f+" | "+t+" | "+a);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.catalog_fragment, container, false);
ImageView imgView = (ImageView) rootView.findViewById(R.id.imgFrag);
Bitmap bmp = BitmapFactory.decodeFile(filename);
imgView.setImageBitmap(bmp);
TextView txtView = (TextView)rootView.findViewById(R.id.title_text);
txtView.setText(author+": "+title);
return rootView;
}
}
My onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog_view);
//Instantiate a ViewPager and a PagerAdapter
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
//populate the fragment
//I was thinking I could pass the fragment class the necessary information to put an imageview and text view in place.
List<Poster> posters = db.getAllPosters();
for (Poster p : posters) {
final String filename = p.getPosterFilename();
//Create a new Fragment?
new CatalogFragment(filename, p.getPosterTitle(), p.getPresenterFname()+" "+p.getPresenterLname());
}
}
However it appears that getItem is what is actually being called to populate the fragment. I think. If I put null in for the arguments that is what I get. If I try to omit this line eclipse starts to complatin. So I am stuck.
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int position){
return new CatalogFragment(null, null, null);
}
#Override
public int getCount(){
return NUM_PAGES;
}
}
The Adapter is what creates the fragments in the view pager, you do not need to create them yourself.
Here is what the Adapter and Activity#onCreate could look like to give you the correct result.
Notice how we are telling the adapter where to get it's data.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog_view);
//Instantiate a ViewPager and a PagerAdapter
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPagetAdapter.setData(db.getAllPosters()); // Provide Poster Data to Adapter
mPager.setAdapter(mPagerAdapter);
}
In the adapter we tell it how to build it's fragments:
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
List<Poster> posterList = new ArrayList<Poster>(0);
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
public void setData(List<Poster> posters) {
posterList.addAll(posters);
}
#Override
public Fragment getItem(int position){
Poster poster = posterList.get(position)
// FRAGMENTS SHOULD NOT IMPLEMENT A CONTRUCTOR!!!
// This will lead to other issue down the line.
return new CatalogFragment(poster.getPosterFilename(), poster.getPosterTitle(), poster.getPresenterFname()+" "+poster.getPresenterLname());
}
#Override
public int getCount(){
return posterList.size();
}
}
```
As a side-note, you should not have a fragment constructor with arguments. See this other SO question for an explanation: Do fragments really need an empty constructor?
I am trying to get some text from editTexts on different fragments. So what I do first is define my mPager and mPagerAdapter:
a_Atenuacion Activity
public class a_Atenuacion extends FragmentActivity{
private static final int NUM_PAGES = 3;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_dat_viewpager);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0:
return new a_Dat_Inicio1();
case 1:
return new a_Dat_Inicio2();
case 2:
return new a_Dat_Inicio3();
}
return null;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
Then I get my 3 fragments classes (Both 1st and 2nd layouts have an editText, but the 3rd one has an editText and a button). The button function is that when I am in the last fragment (fragment3) it take info form (different editTexts) and send to another activity.
a_Dat_Inicio1 Fragment
public class a_Dat_Inicio1 extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.a_dat_inicio1, container, false);
return view;
}
}
a_Dat_Inicio3 Fragment
public class a_Dat_Inicio3 extends Fragment {
EditText edit3;
EditText edit2;
EditText edit1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.a_dat_inicio3, container, false);
edit1 = (EditText)getActivity().findViewById(R.id.editText1);
final String edit11 = edit1.getText().toString();
edit2 = (EditText)getActivity().findViewById(R.id.editText2);
final String edit22 = edit2.getText().toString();
edit3 = (EditText)view.findViewById(R.id.editText3);
final String edit33 = edit3.getText().toString();
Button but=(Button) view.findViewById(R.id.button);
but.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//Creamos el bundle
Bundle bundle = new Bundle();
//Le ponemos la variable parametro con el contenido (key, valor)
bundle.putString("edit3", edit33);
bundle.putString("edit2", edit22);
bundle.putString("edit1", edit11);
Intent net= new Intent(v.getContext(),Prueba1.class);
net.putExtras(bundle);
startActivity(net);
}
});
return view;
}
}
Finally I get bundle on another activity (Prueba1.class) and it is curious that I only get result for editText1 (on 1st fragment) and rest are null.
Can anybody give me a help?
Thanks in advance.
finally I get over an interface, that is the only way I think to get soemthing on already defined fragments.
My code get like this:
1st define an interface
public interface OnEditTextChanged {
public void onEditPressed1(String edit1);
public void onEditPressed2(String edit1);
public void onEditPressed3(String edit1);
Then fragment activity:
public class a_Dat_Inicio1 extends Fragment {
EditText edit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.a_1dat_inicio1, container, false);
init (view);
return view;
}
OnEditTextChanged editListener;
#Override
public void onAttach(Activity activity){
super.onAttach(activity);
try{
editListener=(OnEditTextChanged) getActivity();
}catch(ClassCastException e){
throw new ClassCastException(activity.toString()+"must implemnt onEditPressed");
}
}
private void init(View view) {
edit=(EditText) view.findViewById(R.id.editText1);
//cada vez que se modifique texto llamar
edit.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
final String edit11 = edit.getText().toString();
editListener.onEditPressed1(edit11);
}
});
And finally on our main activity, call the method:
public class a_Atenuacion extends FragmentActivity implements OnEditTextChanged {
String dat1;
String dat2;
String dat3;
private static final int NUM_PAGES = 3;
private PagerAdapter mPagerAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_1dat_viewpager);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
//HERE DO WHATEVER YOU WANT WITH THE DATA CAUGTH ON THE EDIT 1 METHOD
}
#Override
public void onEditPressed1(String edit1) {
if(mPager.getCurrentItem()==0){
dat1=edit1;
Toast bread = Toast.makeText(getApplicationContext(), "edit1", Toast.LENGTH_LONG);
bread.show();
}
}
HOPE this help someone!!!
In any way thanks!!!
Try this:
In your second fragment, pass the intent extras and your value. For example:
In you second fragment, do this :
Intent someintent = new Intent();
//Some first result
someintent.putExtra("your_value_one", your_value_one);
//Some Second result
someintent.putExtra("your_value_two", your_value_two);
//Some third result
someintent.putExtra("your_value_three", your_value_three);
getActivity().setResult(getActivity().RESULT_OK,someintent);
getActivity().finish();
In your other fragment, where you want this result, do this on your other fragment like this:
1) Make some method to get the info.
private void getInfo() {
//Note: The values are coming from a diff activity or fragment and so the strings should match.
Intent data = new Intent();
String first_value = data.getStringExtra("your_value_one");
String second_value = data.getStringExtra("your_value_two");
String third_value = data.getStringExtra("your_value_three");
Log.i("First Value: ", first_value);
Log.i("First Value: ", second_value);
Log.i("First Value: ", third_value);
}
After making this method, just call it on your onActivityCreated().
Now you can use those string however and wherever you want to. If you want to use those values anywhere else, make sure to define you strings at the very start so that you can use the values anywhere.
Hope this answer helps .. :)
This is my first time trying to implement a ViewPager. My app basically has a listing and details page and I'd like the user to be able to swipe between the details. The details are based on an id that I pass in from the listing page. The problem is that I'm calling getActivity() in the details fragment, which is coming back null. Like I said, I'm new to implementing a ViewPager so this might be something obvious:
ListingFragment.class:
public class ListingFragment extends SherlockFragmentActivity
{
private ViewPager mViewPager;
private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
private static List<Fragment> fragments;
public int Id = 0;
#Override
public void onCreate(final Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
mViewPager = (ViewPager)findViewById(R.id.viewpager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mMyFragmentPagerAdapter);
DetailsFragment df1 = new DetailsFragment();
df1.Id = 1;
df1.DisplayItems();
fragments.add(lf);
DetailsFragment df2 = new DetailsFragment();
df2.Id = 2;
df2.DisplayItems();
fragments.add(lf);
}
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
return fragments.get(index);
}
#Override
public int getCount() {
return 2;
}
}
}
DetailsFragment.class
public class DetailsFragment extends SherlockFragmentActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(final Bundle icicle)
{
super.onActivityCreated(icicle);
}
private void DisplayItems()
{
PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("value", false);
...
}
}
UPDATE
I've updated my code to use static newInstance method and a Bundle to pass the id to the details fragment.
ListingFragment.class:
public class ListingFragment extends SherlockFragmentActivity
{
private ViewPager mViewPager;
private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
private static List<Fragment> fragments;
public int Id = 0;
#Override
public void onCreate(final Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
mViewPager = (ViewPager)findViewById(R.id.viewpager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mMyFragmentPagerAdapter);
DetailsFragment df1 = DetailsFragment.newInstance(1);
fragments.add(df1);
DetailsFragment df2 = DetailsFragment.newInstance(2);
fragments.add(df2);
}
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
return fragments.get(index);
}
#Override
public int getCount() {
return 2;
}
}
}
DetailsFragment.class
public class DetailsFragment extends SherlockFragmentActivity
{
private int detailId;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(final Bundle icicle)
{
super.onActivityCreated(icicle);
}
public static DetailsFragment newInstance(int id) {
DetailsFragment lf = new DetailsFragment();
Bundle bundle = new Bundle();
bundle.putInt("id", id);
lf.setArguments(bundle);
return lf;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listing, container, false);
detailId = getArguments().getInt("id");
DisplayItems();
return view;
}
private void DisplayItems()
{
PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("value", false);
...
}
}
You shouldn't be setting variables and calling methods that have to do with initialization from the activity like that.Id should be set using setArguments(...) and using a static newInstance(...) method to instantiate your fragment. You should call displayItems() in onCreateView(...) inside your Fragment, not from the attached Activity.
See the official Android guide on Fragments for more information. You definitely should give the whole thing a read. http://developer.android.com/guide/components/fragments.html