Play with fragments in onTabSelected - android

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.

Related

tabs in viewpager not responding in a sequence android

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.

Android - Swiping without Tabs

NoteActivity Code:
public class NoteActivity extends FragmentActivity implements ActionBar.TabListener
{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Note", "Note Info"};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
TabsPageAdapter.Java
public class TabsPagerAdapter extends FragmentPagerAdapter
{
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index)
{
case 0:
return new NoteFragment();
case 1:
return new NoteInfoFragment();
}
return null;
}
#Override
public int getCount()
{
// get item count - equal to number of tabs
return 2;
}
}
So basically, I used an example of Tabs View and got the Tabs working and it looks like this:
What would I have to do to make it just swipeable without the Tabs showing. An example is like Snapchat. It definitely uses the Swipe view control but the tabs are hidden. Can someone please show me how to get this done?
Since you already have a ViewPager in your code, all you have to do is remove the code that creates the ActionBar tabs (under the comment // Adding tabs), as well as the code that synchronizes the tab selection with the current page (start with the ViewPager.OnPageChangeListener and the ActionBar.TabListener callbacks and see if anything breaks).

Android sherlock FragmentTab clear data

public class PropertyAddActivity extends SherlockFragmentActivity implements OnPageChangeListener, TabListener {
private String TAG="AddActivity: ";
private FileCache fileCache;
private ViewPager mPager;
private ActionBar ab;
private static final int COUNT = 3;
static ArrayList<Integer> mSelectedPropertyType = new ArrayList<Integer>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.property_add);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
mPager.setOnPageChangeListener(this);
ab = getSupportActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowHomeEnabled(false);
ab.setNavigationMode(NAVIGATION_MODE_TABS);
GlobalProperty.getInstance().product=new PropertyAdd();
ab.addTab(ab.newTab().setText("Property").setTabListener(this));
ab.addTab(ab.newTab().setText("Property Detail 1").setTabListener(this));
ab.addTab(ab.newTab().setText("Property Detail 2").setTabListener(this));
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
//listAttachImage.clear();
mSelectedPropertyType.clear();
super.onDestroy();
}
#Override
public void onPageScrolled(int position, float positionOffset,int positionOffsetPixels) {
//AppLog.logString(TAG+"onPageScrolled");
}
#Override
public void onPageSelected(int position) {
//AppLog.logString(TAG+"onPageSelected position"+position);
getSupportActionBar().setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
//AppLog.logString(TAG+"onPageScrollStateChanged ");
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
//AppLog.logString(TAG+"onTabSelected position: "+tab.getPosition());
mPager.setCurrentItem(tab.getPosition());
//ft.replace(R.id.fragment_container, fragment);
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
//AppLog.logString(TAG+"onTabUnselected");
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
//AppLog.logString(TAG+"onTabReselected");
}
public class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
//AppLog.logString(TAG+"COUNT: "+COUNT);
return COUNT;
}
#Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
AppLog.logString(TAG+"position: "+position);
switch (position) {
case 0:
AppLog.logString(TAG+"FRAGGGG1111111");
f = Property1AddFragmentActivity.newInstance(position);
break;
case 1:
AppLog.logString(TAG+"FRAGGGG2222222");
f = Property2AddFragmentActivity.newInstance(position);
break;
case 2:
AppLog.logString(TAG+"FRAGGGGG333333");
f = Property3AddFragmentActivity.newInstance(position);
break;
default:
AppLog.logString(TAG+"Default");
break;
}
return f;
}
}}
i had add add 3 tab Now the problem is that when i set some data in tab1 view and move to tab2 the reselect tab1 the data on tab1 is as it is but when i move to tab3 to tab1 then tab1 data is clear. so cany anyof you have idea to solve this?
At the moment your code appears to be partly action bar tabs and partly ordinary fragments.
If you are trying to use action bar tabs then you need a tab listener. In onTabSelected you should add/replace and in onTabUnselected you should remove.
Please see http://developer.android.com/guide/topics/ui/actionbar.html
section "adding navigation tabs" for a good example.

Tab and fragments no support intents

Hi I have a problem with tab+fragment, here frist I have the class which will create the tab:
public class TestSwipeABActivity extends FragmentActivity {
FragmentTransaction transaction;
static ViewPager mViewPager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Fragment tabOneFragment = new TabOne();
Fragment tabTwoFragment = new TabTwo();
PagerAdapter mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
mPagerAdapter.addFragment(tabOneFragment);
mPagerAdapter.addFragment(tabTwoFragment);
//transaction = getSupportFragmentManager().beginTransaction();
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setOffscreenPageLimit(2);
mViewPager.setCurrentItem(0);
mViewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// When swiping between pages, select the
// corresponding tab.
getActionBar().setSelectedNavigationItem(position);
}
});
ActionBar ab = getActionBar();
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab1 = ab.newTab().setText("Tab One").setTabListener(new TabListener<TabOne>(
this, "tabone", TabOne.class));
Tab tab2 = ab.newTab().setText("Tab Two").setTabListener(new TabListener<TabTwo>(
this, "tabtwo", TabTwo.class));
ab.addTab(tab1);
ab.addTab(tab2);
}
public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
/** Constructor used each time a new tab is created.
* #param activity The host Activity, used to instantiate the fragment
* #param tag The identifier tag for the fragment
* #param clz The fragment's Class, used to instantiate the fragment
*/
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
/* The following are each of the ActionBar.TabListener callbacks */
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(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
public void onTabReselected(Tab arg0,
android.app.FragmentTransaction arg1)
{
// TODO Auto-generated method stub
}
public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1)
{
// TODO Auto-generated method stub
mViewPager.setCurrentItem(arg0.getPosition());
}
public void onTabUnselected(Tab arg0,
android.app.FragmentTransaction arg1)
{
// TODO Auto-generated method stub
}
}
public class PagerAdapter extends FragmentPagerAdapter {
private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
public PagerAdapter(FragmentManager manager) {
super(manager);
}
public void addFragment(Fragment fragment) {
mFragments.add(fragment);
notifyDataSetChanged();
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
}
}
And then here is the fragment for each tab, for example the two one:
public class TabTwo extends Fragment
{
#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.tabtwo, container, false);
Button Activity1= (Button) view.findViewById(R.id.button1);
Activity1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent().setClass(this,ABActivity.class);
startActivity(intent);
}
});
return view;
}
}
The error was: The method setClass(Context, Class<?>) in the type Intent is not applicable for the arguments (new View.OnClickListener(){},
Class<ABActivity>)
I try out changing the context to TabTwo.this, tabtwo.getcontext.this, but nothing, eclipse says to change .setclassName but doesnt work.
If you could help... THANKS!!!
Try this
Intent intent = new Intent(getActivity(),ABActivity.class);
startActivity(intent );
HI I just have a solution:
public void onClick(View view) {
Activity activity = getActivity();
Intent intent = new Intent().setClass(activity, ABActivity.class);
startActivity(intent);
}
Explanation: "Another difference is that a Fragment is not a subclass of Context. This means that a Fragment can not be launched as a component inside your app and therefore always has to live inside of an Activity. This also means that whenever you need a Context inside of a Fragment, you need to get access to the parent Activity. You can do this by using the getActivity() method as we have done in the Fragment button's OnClickListener callback. You need to watch out because getActivity() can return null depending on where the Fragment is in the Activity's lifecycle. So, you should also include a check to see if the Activity is null before you use it."
FROM: http://neilgoodman.net/2012/01/29/working-with-fragments-on-android-part-1/

listviewselection in a listfragment in a tab

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

Categories

Resources