How to call notifydatasetchanged when using SherlockFragmentActivity and ListFragments - android

I have a Sherlock Tab Navigation implemented as the following two classes. Class Dashboard is the main activity. Class DashboardContacts represents one of the tab's fragment.
I am loading HTTP data that gets loaded in a while and when it loads I need the list view in DashboardContacts to reflect the changes brought in from Server and refresh the ListView from blank to filled list. For this i'm calling notifyDataSetChanged method through refresh method in DashboardContacts, but no change relects in ListView until I change the tabs.
public class Dashboard extends SherlockFragmentActivity {
private DashboardContacts contactsTab=new DashboardContacts();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText(getResources().getStringArray(R.array.dashboardTabs)[0]);
tab.setTabListener(new DashboardHome());
getSupportActionBar().addTab(tab);
ActionBar.Tab tab2 = getSupportActionBar().newTab();
tab2.setText(getResources().getStringArray(R.array.dashboardTabs)[1]);
tab2.setTabListener(contactsTab);
getSupportActionBar().addTab(tab2);
//CALLS ASYNCLOADER HERE TO LOAD HTTP DATA
}
private void httpSuccessMethod() {
//Does some work and then calls:
contactsTab.refresh(datasource.getAllContacts());
}
Here is the DashboardContacts class layout:
public class DashboardContacts extends SherlockListFragment implements ActionBar.TabListener{
private UserListAdapter listAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
listAdapter=new UserListAdapter(container.getContext(), ApplicationState.getInstance(container.getContext()).getUserCache());
setListAdapter(listAdapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onStart() {
super.onStart();
}
#Override
public void onTabSelected(Tab tab,android.support.v4.app.FragmentTransaction ft) {
ft.replace(android.R.id.content, this,"contacts");
ft.attach(this);
}
#Override
public void onTabUnselected(Tab tab,android.support.v4.app.FragmentTransaction ft) {
ft.detach(this);
}
#Override
public void onTabReselected(Tab tab,android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void refresh(List<User> list) {
if(this.listAdapter != null) {
this.listAdapter.setList(list);
this.listAdapter.notifyDataSetChanged();
}
}
The refresh method in the custom list adapter looks like, so I am actually changing the data there too:
public void refresh(List<User> list) {
if(this.listAdapter != null) {
this.listAdapter.setList(list);
this.listAdapter.notifyDataSetChanged();
}

I think this is something I didn't implement, so I did it now and solved the issue:
public void setList(List<ExpensePool> list) {
clear();
addAll(list);
notifyDataSetChanged();
}

Related

How to hide android fragment in activity to only have one fragment active at the time?

So basically, i have an activity called (Profile Activity) and two fragments connected to it (Profile view and profile edit fragment). Since im completely new with android studio, java language and fragments, im trying to place both fragments into activity but in a way that only profile view fragment is shown. Edit profile fragment needs to be hidden. Im using next part of the code:
getSupportFragmentManager().beginTransaction().add(R.id.profile_fragment, profileViewFragment).commit();
getSupportFragmentManager().beginTransaction().add(R.id.profile_fragment, profileEditFragment).commit();
I already tried something with "hide" and "show", but with no success. I have imported "android.support.v4.app.FragmentManager;" Thank you.
EDIT:
Profile activity after implementing new code:
public class ProfileActivity extends AppCompatActivity implements ProfileViewFragment.ProfileViewListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
//ovo ispod je za proslijedivanje iz activita u fragment
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
ProfileViewFragment profileViewFragment = new ProfileViewFragment();
ProfileEditFragment profileEditFragment=new ProfileEditFragment();
profileViewFragment.setArguments(getIntent().getExtras());
profileEditFragment.setArguments(getIntent().getExtras());
//getSupportFragmentManager().beginTransaction().add(R.id.profile_fragment, profileEditFragment).commit();
getSupportFragmentManager().beginTransaction().add(R.id.profile_fragment, profileViewFragment).commit();
//getSupportFragmentManager().beginTransaction().replace(R.id.profile_fragment, profileViewFragment).commit();
//FragmentManager fm=getSupportFragmentManager();
//fm.beginTransaction().setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out).show(new ProfileViewFragment()).commit();
//fm.beginTransaction().setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out).show(new ProfileEditFragment()).commit();
//-----------------------------------
}
#Override
public void onOpenProfileEditor() {
ProfileEditFragment profileEditFragment=new ProfileEditFragment();
profileEditFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().replace(R.id.profile_fragment, profileEditFragment).commit();
}
#Override
public void onAttachFragment(Fragment fragment) {
if (fragment instanceof ProfileViewFragment) {
ProfileViewFragment profileFragment = (ProfileViewFragment) fragment;
profileFragment.setListener(this::onOpenProfileEditor);
}
}
}
Profile view fragment with new code:
public class ProfileViewFragment extends Fragment {
private Unbinder unbinder;
//novi kod sa stacka
private ProfileViewListener listener;
//-------------
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
FragmentProfileViewBinding viewBinding=DataBindingUtil.inflate(inflater, R.layout.fragment_profile_view, container, false);
View view=viewBinding.getRoot();
unbinder = ButterKnife.bind(this, view);
UserModel user = (UserModel) getArguments().get(ModelEnum.UserModel.name());
//viewBinding povezuje fragment i xml (proslijeduje user)
viewBinding.setUser(user);
//viewBinding.setUserGender(user);
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
// #OnClick(R.id.btn_change_settings)
// public void changeSettings(){
//getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.profile_fragment, new ProfileEditFragment()).commit();
// }
#Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
//ISPOD JE NOVI KOD SA STACK OVERFLOWA
public interface ProfileViewListener{
void onProfileEditor();
}
public void setListener(ProfileViewListener listener) {
this.listener = listener;
}
#OnClick(R.id.btn_change_settings)
public void onEdit(View view){
if(listener!=null){
onOpenProfileEditor();
}
}
}
Instead of
getSupportFragmentManager().beginTransaction().add(R.id.profile_fragment, profileEditFragment).commit();
It must be
getSupportFragmentManager().beginTransaction().replace(R.id.profile_fragment, profileEditFragment).commit();
This will replace your fragment, instead of adding it.
Please, also note that you must call "add" for the first time and use "replace" afterwards.
You may find more about fragments here: https://developer.android.com/training/basics/fragments/fragment-ui
EDIT
For the new issue you have outlined, the solution is to "report" you activity that an event had happened, so it can take action. Here is how to do that.
First, we need an interface (you can add it inside you Profile fragment) and to link the activity to our fragment, if it implements that interface.
public class ProfileViewFragment extends Fragment {
...
...
private ProfileViewListener listener;
...
...
#OnClick(R.id.btn_change_settings)
public onEdit(View view) {
// If there is anyone listening, report that we need to open editor
if (listener != null) {
listener .onOpenProfileEditor();
}
}
public void setListener(ProfileViewListener listener) {
this.listener = listener;
}
// The interface
public interface ProfileViewListener {
void onOpenProfileEditor();
}
}
And in the class, we need to implement the interface and subscribe as a listener.
public class ProfileActivity extends AppCompatActivity implements ProfileViewFragment.ProfileViewListener {
...
...
#Override
public void onOpenProfileEditor() {
ProfileEditFragment profileEditFragment=new ProfileEditFragment();
profileEditFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager()
.beginTransaction
.replace(R.id.profile_fragment, profileEditFragment)
.commit();
}
#Override
public void onAttachFragment(Fragment fragment) {
if (fragment instanceof ProfileViewFragment) {
ProfileViewFragment profileFragment = (ProfileViewFragment) fragment;
profileFragment.setListener(this);
}
}
}
You may find more detail on Activity-Fragment communication here - https://developer.android.com/training/basics/fragments/communicating

How to properly get RecyclerView Adapter from Fragment?

I have a Fragment that displays a list using my class ListRecyclerViewAdapter that extends RecyclerView.Adapter:
public class ListFragment extends Fragment {
private ListRecyclerViewAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
adapter = new ListRecyclerViewAdapter(context, itemsToDisplay);
...
}
public ListRecyclerViewAdapter getListAdapter() {
return adapter;
}
}
When I try to get the Adapter from an Activity that has the above Fragment, I get null:
public class DataActivity extends FragmentActivity {
#Override
protected void onResume() {
super.onResume();
listFragment = new ListFragment(itemsToDisplay);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_list_container, listFragment).commit();
ListRecyclerViewAdapter wantedAdapter = listFragment.getAdapter();
}
}
Why is that? It looks to me like the Fragment's method onCreateView() was not called at the point when I call listFragment.getAdapter().
Fragment is not created when you are trying to call the method.So check is the fragment is attached before accessing the adapter.
public ListRecyclerViewAdapter getListAdapter() {
if(this.isAdded() && adapter!=null){
return adapter;
}
return null;
}
Also load the fragment in oncreate of activity and call the adpter reference method in onresume
public class DataActivity extends FragmentActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listFragment = new ListFragment(itemsToDisplay);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_list_container, listFragment).commit();
}
#Override
protected void onResume() {
super.onResume();
ListRecyclerViewAdapter wantedAdapter = listFragment.getAdapter();
}
}
As soon as it created, it called listfragment adapter. so, the UI creation will take some time , but the flow flowed to that line. so it return null. you can acheive this in following way.
public class ListFragment extends Fragment {
LoadAdapter loadAdapter;
private ListRecyclerViewAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
adapter = new ListRecyclerViewAdapter(context, itemsToDisplay);
...
}
public ListRecyclerViewAdapter getListAdapter() {
return adapter;
}
interface LoadAdapter {
public void onAdapterLoaded();
}
public void setOnAdapterLoad(LoadAdapter loadAdapter){
this.loadAdapter = loadAdapter;
}
#Override
public void onResume(){
loadAdapter.onAdapterLoaded();
}
}
Now the activity code as follows:
public class DataActivity extends FragmentActivity {
#Override
protected void onResume() {
super.onResume();
listFragment = new ListFragment(itemsToDisplay);
listFragment.setOnAdapterLoad(new LoadAdapter(){
#Override
public void onAdapterLoaded(){
ListRecyclerViewAdapter wantedAdapter = listFragment.getAdapter();
}
});
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_list_container, listFragment).commit();
}
}
Hope this helps.

Android: Fragments in FragmentPagerAdapter do not display data in landscape after device turned on

I had created tabview with swipe using Fragments and FragmentPagerAdapter. In the fragment's hosting activity, I had added a fragment and that fragment shows tabview using TabHost. 1st tab has a listview that displays data from DB using CursorLoader and the 2nd one is mapView.
Everything works fine in portrait mode.
Problem occurs in following case:
User is using app in landscape mode. He opened tabbed view screen. Data is displayed in the listview from the cursor loader. Mapview is also displayed in the second tab. So far everything is working as required.
He left the app as is.(Means, he did not pressed back button, home button or switched to another app)
Screen went off after some time.
When user unlocks the device, my app's tabbed view will be visible again. But,now listview does not show any data and mapview also disappeared.
ClientListFragment
public class ClientListFragment extends SwipeRefreshListFragment {
private static final String TAG = "ClientListFragment";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate");
setHasOptionsMenu(true);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setColorScheme(R.color.gplus_color_1, R.color.gplus_color_2,
R.color.gplus_color_3, R.color.gplus_color_4);
}
#Override
public void onResume() {
super.onResume();
Log.e(TAG, "onResume");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e(TAG, "onActivityCreated");
}
#Override
public void onPause() {
// Log.i(TAG, "onPause");
super.onPause();
}
#Override
public void onStop() {
super.onStop();
Log.e(TAG, "onStop");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy");
}
}
Map Fragment
public class ClientMapFragment extends Fragment {
private static final String TAG = "ClientMapFragment";
private GoogleMap googleMap;
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.map_fragment, container, false);
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);
return v;
}
#Override
public void onStart() {
super.onStart();
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onStop() {
super.onStop();
}
#Override
public void onPause() {
super.onPause();
}
}
Activity
public class ClientActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean isTablet = getResources().getBoolean(R.bool.isTablet);
if (isTablet)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
else
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
setContentView(R.layout.activity_clients);
ClientFragment clientFragment = new ClientFragment();
Bundle bundle = new Bundle();
bundle.putBoolean("ShouldChangeActionBarTitle", true);
clientFragment.setArguments(bundle);
getFragmentManager()
.beginTransaction()
.add(R.id.fragmentContainer, clientFragment,
getResources().getString(R.string.clients_fragment))
.commit();
}
}
FragmentpagerAdapter
public class TabsPagerAdapter extends FragmentStatePagerAdapter {
private static final String TAG = "TabsPagerAdapter";
private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
FragmentManager fm;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
this.fm = fm;
mFragments.add(new ClientListFragment());
mFragments.add(new ClientMapFragment());
}
#Override
public Fragment getItem(int index) {
Log.e(TAG, "getItem");
return mFragments.get(index);
}
#Override
public int getCount() {
return mFragments.size();
}
}
Update 1
So, after doing hours of research I came to a conclusion that, it is problem with fragment's state in the FragmentPagerAdapter. When screen is turned off fragmentpager's saveState is called.. When the device is unlocked again, fragment's will be restored from previous state. I verified that LoaderManager callbacks are called, and data was also fetched from DB. But, didn't appeared on screen.
I didn't even bothered about looking at my activities code at once. Everytime, a new fragment was created in onCreate(). The fix is very simple.
if (savedInstanceState == null) {
Log.e(TAG, "onCreate savedInstanceState == null");
ClientFragment clientFragment = new ClientFragment();
Bundle bundle = new Bundle();
bundle.putBoolean("ShouldChangeActionBarTitle",
shouldChangeActionBarTitle);
clientFragment.setArguments(bundle);
getFragmentManager()
.beginTransaction()
.add(R.id.fragmentContainer, clientFragment,
getResources().getString(R.string.clients_fragment))
.commit();
}

onCreate invoke multiple times in fragment in android

I have a ListView in my activity.On clicking on the list item it invokes another activity.In that activity I have implemented ViewPager and fragments.
When it loads first time onResume() ,onCreate() and onCreateView() method called twice, if I clicks on first list item. (i.e. it loads first and second fragment view)
when I click on the any other List fragment except first then it calls onResume() ,onCreate() and onCreateView() methods three times (i.e. It loads previous and after and click view )
It is absoutely fine but I have google analytics code with which I have to track only current page so where I can put this code to load for only current page
My question is my googleAnalytics code tracs three or two pages at first time even user doesnot gone through those pages how to avoid this ?
My code is as below for fragment
public class MainListActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate()");
CustomFragmentPagerAdapter adapter = new CustomFragmentPagerAdapter();
viewPager.setAdapter(adapter);
}
}
//code for fragment adapter
public class CustomFragmentPagerAdapter extends FragmentPagerAdapter {
public CustomFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
CustomFragment customFragment = new CustomFragment();
arrayList.add(customFragment);
return customFragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return arrayList.size();
}
}
//code for fragment
public class CustomFragment extends Fragment{
public CustomFragment() {
super();
}
#Override
public void onResume() {
super.onResume();
Log.v(TAG, "onCreate -Resume");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
Log.v(TAG, "onCreateView");
return myAnyView;
}
}
The problem is that the onResume() method is called for all your fragments, that is including the invisible ones.
Check gorn's answer here:
How to determine when Fragment becomes visible in ViewPager
You have to override
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// Your logic
}
and do your logic in there.

FragmentActivity crashing on orientation change (MainActivity seen as Fragment)

I am using a SherlockFragmentActivity with tabs. It loads fine the first time, but when the orientation gets changed, the following error occurs:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{ext.domain.app/ext.domain.app.MainActivity}:
android.support.v4.app.Fragment$InstantiationException: Unable to instantiate
fragment ext.domain.app.MainActivity$1: make sure class name exists, is public,
and has an empty constructor that is public
I've been looking at posts about similar problems, but there is a difference: in this error it seems like MainActivity is seen as a Fragment ("unable to instantiate fragment ext.domain.MainActivity"), instead of the used Fragment class being mentioned.
It does not help to give MainActivity an empty constructor. The Fragments that I actually use are called ArticleListFragment and it is a public class with an empty constructor.
I'm running the app on Android 2.3, below is the code (stripped of most irrelevant details and still throwing the exception):
MainActivity.java:
(Edit: added the instantiation of the ArticleListFragment which is an anonymous inner type. It seems that if I remove it, it does work.)
public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {
ActionBar actionBar;
public MainActivity() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// Create ActionBar
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create Tabs
String[] tabs = {
getString(R.string.tab1),
getString(R.string.tab2),
getString(R.string.tab3),
getString(R.string.tab4)
};
for(String tabname : tabs) {
ActionBar.Tab tab = actionBar.newTab();
tab.setText(tabname);
tab.setTabListener(this);
actionBar.addTab(tab);
}
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
switch(tab.getPosition()) {
case 0:
ArticleListFragment home = new ArticleListFragment() {
public void onAttach(Activity activity) {
// This is some code to populate the Fragment with an HTTPRequest
super.onAttach(activity);
RequestParams paramsHome = RequestClient.getBasicRequestParams(activity);
populate("frontpage", paramsHome);
};
};
ft.replace(R.id.contentframe, home);
break;
case 3:
ArticleListFragment saved = new ArticleListFragment() {
public void onAttach(Activity activity) {
// This is some code to populate the Fragment with an HTTPRequest
super.onAttach(activity);
RequestParams paramsSaved = RequestClient.getBasicRequestParams(activity);
populate("saved", paramsSaved);
};
};
ft.replace(R.id.contentframe, saved);
break;
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
ArticleListFragment.java:
public class ArticleListFragment extends SherlockFragment {
public ArticleListFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.fragment_articlelist, null);
return layout;
}
}
Any ideas why this code is wrong?
What's the full code for MainActivity? It isn't complaining about trying to instantiate MainActivity, but an anonymous inner class (indicated by the $1) of MainActivity.

Categories

Resources