As i am new to android I stuck to some point while implementing ActionBarTab with swipable viewpager using fragments
I have 3 tabs each with some controls last tab will submit all tabs data to one table
i navigate through next button and with tab change event
using next tab i can set data to Class object using getter/Setter and i submit that class data to DB table for save ..
now i want to call same method while tab change event
method is SetdataToModelClass()
How to call same method on tab change event which is already called on button click event so if user navigate through tabs instead next button and change any data My SetdataToModelClass() is called
SetdataToModelClass is in every fragment,where as my Tab change event is in MainFragmentActivity class so how to call SetdataToModelClass() method on tab change event (i.e want to have communication between MainFragment Act & Fragment)
Code for MainActivity Class is here:
public class TestFragmentTabHost extends FragmentActivity implements TabListener {
ViewPager vp;
ActionBar ab;
SalesActivity sa = new SalesActivity();
SessionManager session = null;
String usrNm = null;
String szImeiId = null;
Spinner spnAECust;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost_act);
/* Action Bar Color change on create*/
ActionBar actionBar = getActionBar();
ActionBarColor.setBackgroundColor(actionBar);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setTitle(Html.fromHtml("<font color=\"white\" face=\"verdana,arial\">" + getString(R.string.air) + "</font>"));
// Session Manager
session = new SessionManager(getApplicationContext());
/* To Get Unique Device id */
TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
szImeiId = TelephonyMgr.getDeviceId(); // Requires READ_PHONE_STATE.
// get User Details from Session
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap = session.getUserDetails();
usrNm = hashMap.get(SessionManager.KEY_USRNM);
vp = (ViewPager) findViewById(R.id.pager);
vp.setAdapter(new MyPageAdapter(getSupportFragmentManager()));
ab = getActionBar();
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = ab.newTab();
tab1.setText("INFO");
tab1.setTabListener(this);
ActionBar.Tab tab2 = ab.newTab();
tab2.setText("PORT");
tab2.setTabListener(this);
ActionBar.Tab tab3 = ab.newTab();
tab3.setText("PACKAGES");
tab3.setTabListener(this);
ab.addTab(tab1);
ab.addTab(tab2);
ab.addTab(tab3);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
vp.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
// For Page adapter
class MyPageAdapter extends FragmentPagerAdapter {
public MyPageAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int arg0) {
Fragment frgmnt = null;
if (arg0 == 0) {
frgmnt = new InfoFragment();
}else if (arg0 == 1) {
frgmnt = new PortFragment();
}else if (arg0 == 2) {
frgmnt = new PackagesFragment();
}
return frgmnt;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
public void setSalesInfoData(List<String> sales) {
while (sales.size() > 0) {
sa.setCustomerId(Integer.parseInt(sales.get(0).toString()));
sa.setProspectId(Integer.parseInt(sales.get(1).toString()));
sa.setCommodityId(Integer.parseInt(sales.get(2).toString()));
sa.setSpecialNotes(sales.get(3));
sa.setLob(sales.get(4));
DateFormat dt = new DateFormat();
Date crTs = dt;
sa.setCrTs(crTs);
sa.setCrUsr(usrNm);
sa.setDeviceId(szImeiId);
break;
}
}
using method to setdata to Class SalesActivity
change adapter to:
class MyPageAdapter extends FragmentPagerAdapter {
Fragment[] fragments=new Fragment[3];
public MyPageAdapter(FragmentManager fm) {
super(fm);
fragments[0]= new InfoFragment();
fragments[1]= new PortFragment();
fragments[2]= new PackagesFragment();
}
#Override
public Fragment getItem(int arg0) {
return fragments[arg0];
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
And in onTabUnselected get the fragment call the function ex:
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Fragment fragment=adapter.getItem(tab.getPosition());
if(fragment instanceof InfoFragment){
((InfoFragment)fragment).SetdataToModelClass();
}
if(fragment instanceof PortFragment){
((PortFragment)fragment).SetdataToModelClass();
}
if(fragment instanceof PackagesFragment){
((PackagesFragment)fragment).SetdataToModelClass();
}
}
Also make adapter object a class variable ex:
ontside onCreate
MyPageAdapter adapter;
in onCreate
adapter=new MyPageAdapter(getSupportFragmentManager());
vp.setAdapter(adapter);
Related
In my android app I am using a tab swipe view having 4 tabs first tab displays data on the UI and is calling a WS via AsyncTask, data returned by the WS is populating a pojo and this pojo is passed to other tab(fragments)
The problem I face is Tabs are not executed in a sequence when I click on first tab, first and 2 tab are executing before calling WS due to this strange behaviour I cant get data in my second tab
Activity:
public class TabSwipeActivity extends FragmentActivity implements ActionBar.TabListener, DetailsFoeFragment.ExistingFeatureDataInt
{
ActionBar actionbar;
ViewPager viewpager;
FragmentPageAdapter ft;
ExistingData existingData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabswipe);
ArrayList<String> detailList =this.getIntent().getStringArrayListExtra("FeatureData");
viewpager = (ViewPager) findViewById(R.id.pager1);
ft = new FragmentPageAdapter(getSupportFragmentManager());
if(detailList!=null)
ft.setDetailList(detailList);
// ft.setExistingData(getExistingData());
actionbar = getActionBar();
viewpager.setAdapter(ft);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
actionbar.addTab(actionbar.newTab().setText("Details").setTabListener(this),0,true);
actionbar.addTab(actionbar.newTab().setText("Action Taken").setTabListener(this),1,false);
actionbar.addTab(actionbar.newTab().setText("Before Photos").setTabListener(this),2,false);
actionbar.addTab(actionbar.newTab().setText("After Photos").setTabListener(this),3,false);
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onPageSelected(int arg0) {
actionbar.setSelectedNavigationItem(arg0);
}
});
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewpager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
////
Adapter:
/////
public class FragmentPageAdapter extends FragmentPagerAdapter {
Bundle bundle= new Bundle();
ExistingData existingData = new ExistingData();;
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
Fragment fragment;
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
fragment = new DetailsFoeFragment();
bundle.putStringArrayList("details",detailList);
fragment.setArguments(this.bundle);
return fragment;
case 1:
fragment= new ActionFoeFragment();
bundle.putParcelableArrayList("actions",(ArrayList)existingData.getActionList());
// bundle.putParcelableArrayList("actions",(ArrayList)existingData.getActionList());
// bundle.putString("featureId",detailList.get(2));
fragment.setArguments(this.bundle);
return fragment;
case 2:
fragment = new PhotoBeforeFoeFragment();
// bundle.putStringArrayList("beforeImage",null);
bundle.putStringArrayList("beforeImage",(ArrayList<String>)existingData.getBeforeImagePath());
fragment.setArguments(this.bundle);
return fragment;
case 3:
fragment = new PhotoAfterFoeFragment();
bundle.putStringArrayList("afterImage",(ArrayList<String>)existingData.getAfterImagePath());
fragment.setArguments(this.bundle);
return fragment;
default:
break;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
ArrayList<String> detailList;
public void setDetailList(ArrayList<String> detailList) {
this.detailList = detailList;
}
public void setExistingData(ExistingData existingData) {
this.existingData = existingData;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
Please provide inputs
thanks
One solution:
In your activity, run WS with Asynctask, and then call the fragments that need data to update view
class yourActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
... ...
//set up your tabs with fragments
... ...
//WS: Your asynctask requesting data from server
(new AsyncTask<... ...>() {
... ...
#Override
protected void onPostExecute(dataSet) {
//here you have got your data set from server then inform tabs to update
YourFragment frag = (YourFragment)getSupportFragmentManager().findFragmentById(R.id.your_fragment);
frag.updateView(dataSet);
}
}).execute();
}
}
In each tab/fragment that needs shared data set, define a method to update view with the data set
class YourFragment extebds Fragment{
... ...
public void updateView(dataSet)
{
//update this fragment view with dataSet
//and stop your waiting dialog
}
}
Hope this solution works for you.
I am developing an app which is using ActionBar.NAVIGATION_MODE_TABS and a separate FragmentPagerAdapter class in order to swipe through 5 different screens. On these screens there are buttons you can press to load a new activity, and then go back to the MainActivity when done. My problem is, every time you go back and reload the MainActivity it defaults back to the first tab (0).
I have tried many different solutions to try and get the previously selected tab to load when the activity is reloaded including SharedPreferences, saving instance state and trying to force the FragmentPagerAdapter class to load a certain fragment when called - but something keeps overriding it and it always goes back to the first tab (0) without fail. I would be really grateful for any possible solutions to this.
Code as follows:
public class MainActivity extends FragmentActivity implements TabListener {
ActionBar tabBar;
ViewPager viewPager;
#Override
protected void onCreate(Bundle currenttab) {
super.onCreate(currenttab);
setContentView(R.layout.activity_main);
// Sets up view pager
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setCurrentItem(0);
viewPager.setAdapter(new MainAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int currenttab) {
tabBar.setSelectedNavigationItem(currenttab);
}
#Override
public void onPageScrolled(int currenttab, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int currenttab) {
}
});
// Adds tabs to action bar
tabBar = getActionBar();
tabBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = tabBar.newTab();
tab1.setIcon(R.drawable.action);
tab1.setTabListener(this);
ActionBar.Tab tab2 = tabBar.newTab();
tab2.setIcon(R.drawable.jobs);
tab2.setTabListener(this);
ActionBar.Tab tab3 = tabBar.newTab();
tab3.setIcon(R.drawable.historic);
tab3.setTabListener(this);
ActionBar.Tab tab4 = tabBar.newTab();
tab4.setIcon(R.drawable.breeds);
tab4.setTabListener(this);
ActionBar.Tab tab5 = tabBar.newTab();
tab5.setIcon(R.drawable.special);
tab5.setTabListener(this);
tabBar.addTab(tab1);
tabBar.addTab(tab2);
tabBar.addTab(tab3);
tabBar.addTab(tab4);
tabBar.addTab(tab5);
}
// Action bar methods
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
// Class to deal with swipe function
class MainAdapter extends FragmentPagerAdapter {
public MainAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int currenttab) {
// TODO Auto-generated method stub
Fragment fragment = null;
if (currenttab == 0) {
fragment = new FragmentAction();
}
if (currenttab == 1) {
fragment = new FragmentJobs();
}
if (currenttab == 2) {
fragment = new FragmentHistory();
}
if (currenttab == 3) {
fragment = new FragmentBreeds();
}
if (currenttab == 4) {
fragment = new FragmentSpecial();
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 5;
}
}
I managed to resolve this by making my back buttons behavior mimic the built in behavior of calling onBackPressed();. The problem was going back using my previous code was creating a new instance of my MainActivity thus defaulting it to the first tab.
Dears,
I searched for this issue for more than a day but with no luck.
I implement exactly the code posted here:
Adding Navigation Tabs
My code for onTabSelected look like:
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(R.id.alert_fragment_container, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
// prepare adapter for ExpandableListView
Log.i("After Adapter Created", "Passed");
final ExpandableListAdapter expListAdapter = new AlertsAdapter(
mActivity, myAlerts, violations);
Log.i("After Adapter Initialized", "Passed");
((MyCustomFragment)mFragment).violations.setAdapter(expListAdapter);
}
The code is working fine till last line, where I need to set the adapter for public static list initialized in MyCustomFragment in onCreateView, here my code for fragment:
public class MyCustomFragment extends Fragment {
public MyCustomFragment() {
}
public static ExpandableListView violations;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_alerts_poi, container, false);
violations = (ExpandableListView) rootView.findViewById(R.id.POIAlertList);
Log.i("onCreateView POI", "Called");
return rootView;
}
}
It give Null pointer error. With my debugging logs, I notice that this log Log.i("onCreateView POI", "Called"); appears after this Log.i("After Adapter Initialized", "Passed");. This means that I'm trying to set the adapter for a fragment isn't initialized yet.
This is the exact problem I'm face, I need to fed the ExpandableListView with data based on Tab selection in onTabSelected.
What I'm doing wrong? What is the best solution?
Regards,
It seems that you need a ViewPager, I just implemented a navigation tabs few days ago, here is my code, it navigates between 4 fragments:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
private ActionBar actionBar;
private ViewPager mViewPager;
private AppSectionsPagerAdapter mAppSectionsPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
actionBar=getActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon1).setTabListener(this));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon2).setTabListener(this));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon3).setTabListener(this));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon4).setTabListener(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.action_menu, menu);
return true;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
And here is the adapter:
public class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
case 3:
return new Fragment4();
}
return null;
}
#Override
public int getCount() {
return 4;
}
}
Have a look # this Tablayout.onTabselected for latest API updates. ActionBar.TabListener is a old implementation.
im trying to make a basic ui in android with tabs. however i keep getting a nullpointer exception whenever i try to run it. the error originates from initializing viewpager. (i dont know what else to type and i keep receiving an error whenever i try to post.)
public class MainActivity extends FragmentActivity implements TabListener {
ActionBar actionBar;
ViewPager viewPager;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_main);
viewPager=(ViewPager) viewPager.findViewById(R.id.tabs);
viewPager.setAdapter(new adapter(getSupportFragmentManager()));
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab stocks = actionBar.newTab();
stocks.setText("Stocks");
stocks.setTabListener(this);
ActionBar.Tab market = actionBar.newTab();
market.setText("Market");
market.setTabListener(this);
ActionBar.Tab portfolio = actionBar.newTab();
portfolio.setText("Portfolio");
portfolio.setTabListener(this);
actionBar.addTab(stocks);
actionBar.addTab(market);
actionBar.addTab(portfolio);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainsearch, menu);
return true;
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
class adapter extends FragmentPagerAdapter {
public adapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
public Fragment getItem(int arg0) {
Fragment fragment = null;
if(arg0==0) {
fragment = new StocksFragment();
}
if (arg0==1){
fragment = new MarketFragment();
}
if (arg0==2){
fragment = new PortfolioFragment();
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
}
Of course this line will give a NPE
viewPager=(ViewPager) viewPager.findViewById(R.id.tabs);
you are calling a method on an object that you are trying to initialize (calling findViewById() on null viewPager)
If tabs is in activity_main.xml then just remove viewPager
viewPager=(ViewPager) findViewById(R.id.tabs);
findViewById is a method of the activity, not of the view. So it should be:
viewPager=(ViewPager) this.findViewById(R.id.tabs);
I have the following problem. I have an app with a viewpager and two tabs. The two tabs are two listfragments. Now I want that if the user selects an item in the first tab, that the 5th item is selected in the second tab. (the second tab is the translation of text from the first item). My problem is that when i select an item in the first tab, and now switch to the second tab, there is nothing selected.
When I click something in the first tab I call a function in the Parent activity which calls a function in the second fragment which should select the 5th Item.
This is the code of the Fragment activity:
public class Dailyquran extends FragmentActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
public ArrayList<Tweet> tweets = new ArrayList<Tweet>();
public ArrayList<Tweet> tweets2 = new ArrayList<Tweet>();
//private static ArrayList<String> roomList;
public String a;
public static int laenge_inhalt;
public static int selected;
public static String sure_media;
public static String vers_media;
String koran_filename;
//int tabwahl=0;
int playstatus=0;
MediaPlayer mp = new MediaPlayer();
MenuItem playMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_dailyquran, menu);
playMenu = menu.findItem(R.id.menu_play);
return true;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Daily Quran");
setContentView(R.layout.activity_dailyquran);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the action bar.
final ActionBar actionBar = getActionBar();
// set the app icon as an action to go home
actionBar.setDisplayHomeAsUpEnabled(true);
//enable tabs in actionbar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// 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 (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
// listener for when this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
main();
}
/* WEITER UNTEN SIND DIE TABSELECTED FUNKTIONEN !!!!
//#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
// #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 onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private FragmentTransaction mCurTransaction = null;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE; //To make notifyDataSetChanged() do something
}
#Override
public Fragment getItem(int i) {
// Fragment building_fragment = new BuildingFragment();
Fragment room_fragment = new RoomFragment();
Fragment transl_fragment = new TransliterationFragment();
// Fragment device_fragment = new DeviceFragment();
Bundle args = new Bundle();
switch(i){
case 0:
room_fragment.setArguments(args);
return room_fragment;
case 1:
return transl_fragment;
case 2:
// args.putInt(RoomFragment.ARG_SECTION_NUMBER, i);
// room_fragment.setArguments(args);
return room_fragment;
default: return null;
}
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return "a";
case 1: return "b";
case 2: return "c";
}
return null;
}
#Override
public Object instantiateItem(View container, int position) {
if (mCurTransaction == null) {
mCurTransaction = getSupportFragmentManager().beginTransaction();
}
// TODO Auto-generated method stub
Fragment fragment = getItem(position);
if (fragment!=null){
System.out.println("Fragment Found!");
mCurTransaction.attach(fragment);
}
return fragment;//super.instantiateItem(container, position);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_play:
play();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem switchButton = menu.findItem(R.id.menu_play);
}
public class Tweet {
String content;
String sure;
String vers;
}
public class Tweet2 {
String content;
String sure;
String vers;
}
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
//HERE IS THE CODE FOR SELECTING THE SECOND ITEM
public void one_changed()
{
TransliterationFragment fragment_meaning = (TransliterationFragment) getSupportFragmentManager().findFragmentById(R.id.myfragment2);
Toast.makeText(getApplicationContext(),"Change Selection", Toast.LENGTH_SHORT).show();
fragment_meaning.change_selected(); // do what updates are required
}
AND THIS IS THE RELEVANT CODE OF THE SECOND FRAGMENT, I can see the string "Hallo" is in the console, so the function is really called, but i the Item isnt selected.
public void change_selected()
{
System.out.println("Hallo");
ListView list=getListView();
list.setSelection(4);
}
Its working now when i add this code in the function on_changed in the parent activity:
TransliterationFragment fragment_meaning = (TransliterationFragment) getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.pager+":1");
fragment_meaning.change_selected(); // do what updates are required