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 .. :)
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 have a fragment activity that have 4 page and load from 4 link from internet
when program start customlist show lists but when i change page the view show white page
my fragment :
public class VpiAbsTestActivitynouser extends SherlockFragmentActivity {
private static final String[] CONTENT = new String[] {" page1","page2","page3","page4"};
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
TextView mSearchView;
public String[] xmlURLArray = new String[]{"link1.xml","link2.xml","link3.xml","link4.xml"};
protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_tabs);
Intent i = new Intent(this, transparentone.class);
startActivity(i);
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
mIndicator.setCurrentItem(1);
}
class TestFragmentAdapter extends FragmentPagerAdapter {
private int mCount = CONTENT.length;
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Bundle args = new Bundle();
args.putString("url", xmlURLArray[position]);
return customlist.newInstance(args);
}
#Override
public int getCount() {
return mCount;
}
public CharSequence getPageTitle(int position) {
return VpiAbsTestActivitynouser.CONTENT[position % VpiAbsTestActivitynouser.CONTENT.length].toUpperCase();
}
#Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
}
}
and my custom list code is :
public class customlist extends SherlockFragment {
int fragment_position_in_viewpager = 0;
// All static variables
public static customlist newInstance(Bundle args) {
customlist fragment = new customlist();
fragment.setArguments(args);
return fragment;
}
ListView list;
LazyAdapterbeth adapter1;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
URL = getArguments().getString("url");
}
new getFeed().execute();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(
R.layout.dovomi, container, false);
return root;
}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (getArguments() != null) {
fragment_position_in_viewpager = getArguments().getInt("position");
//Update the Fragment on ViewPager Position.
}
}
private class getFeed extends AsyncTask<Void, Void, Document> {
#Override
protected void onPreExecute() {
}
protected Document doInBackground(Void... params) {
}
protected void onPostExecute(Document doc) {
}
list=(ListView)getView().findViewById(R.id.list);
adapter1=new LazyAdapterbeth(getActivity(), songsList);
adapter1.notifyDataSetChanged();
list.setAdapter(adapter1);
how i can refresh view when change the view from page 1 to page 2 ?
As your code is very long, I didn't analyse it deeply but the following solutions come to my mind:
1) onCreateView for each fragment method is regularly called when fragment is recreated/its view hierarchy is recreated
Thus it can be one place for refreshing. However onCreateView is called slightly earlier than before paging displaying (usually where you display page nr 2, onCreateView for page number 3 is executed). The best idea is to put a short code displaying Toast in onCreateView and then you will know if it's ok for you.
2) Your main activity (extending FragmentActivity) can get 3 methods which are executed when swiping between fragments. It will look more or less in the following way:
public MyActivity extends FragmentActivity implements OnPageChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainActivityPageAdapter = new MainActivityPageAdapter (this, getSupportFragmentManager());
mainActivityViewPager = (ViewPager) findViewById(R.id.main_activity_view_pager);
mainActivityViewPager.setAdapter(mainActivityPageAdapter);
//set listener (for navigator)
mainActivityViewPager.setOnPageChangeListener(this);
}
//and now you have 3 methods
#Override
public void onPageScrollStateChanged(int state) {
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
You can read more http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html
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.
Any idea on how to set separate activity on each page on ViewPager. I try compatibility package by using FragmentActivity, but on the sample given, it just one type which is ListFragment.
The code I copied from sample of compatibility v4:
public class ViewPagerListActivity extends FragmentActivity {
static final int NUM_ITEMS = 10;
MyAdapter mAdapter;
ViewPager mPager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
// Watch for button clicks.
Button button = (Button)findViewById(R.id.goto_first);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(0);
}
});
button = (Button)findViewById(R.id.goto_last);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(NUM_ITEMS-1);
}
});
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
return ArrayListFragment.newInstance(position);
}
}
public static class ArrayListFragment extends ListFragment {
int mNum;
/**
* Create a new instance of CountingFragment, providing "num"
* as an argument.
*/
static ArrayListFragment newInstance(int num) {
ArrayListFragment f = new ArrayListFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
/**
* When creating, retrieve this instance's number from its arguments.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
/**
* The Fragment's UI is just a simple text view showing its
* instance number.
*/
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("Fragment #" + mNum);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings));
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
}
}
}
The following execution:
I would like to put activity or view with specific UI and process on each ViewPager, not just one type ListView like on above code sample. for example page 1 is map information; page 2 is combination TextView, EditView and button, Page 3 is Gallery; and so on. Any idea to implement it? or is there any tutorial for this?