I created a tabbed activity called UserProfile.java
. As many of you know, this kind of activity comes with a built-in class called PlaceholderFragment
which is basically where the magic happens. Mine looks as follows:
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Some code related to database queries irrelevant to this question...
if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
View profileView = inflater.inflate(
R.layout.fragment_user_profile_data,
container,
false
);
// Here's some code intended to get edittext's values irrelevant to this question...
final Button saveProfile = (Button) profileView.findViewById(R.id.profile_save_data);
saveProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
UpdateProfileForm upf = new UpdateProfileForm(
userName.getText().toString(),
userLastName.getText().toString(),
userID.getText().toString(),
userPhone.getText().toString(),
userEmail.getText().toString(),
userAddress.getText().toString()
);
new UpdateProfile().execute(upf);
view.setVisibility(View.GONE);
}
});
return profileView;
} else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
View profileView = inflater.inflate(R.layout.another_fragment, container, false);
return profileView;
} else {
View profileView = inflater.inflate(R.layout.fragment_user_profile_data, container, false);
return profileView;
}
}
}
As you can see, I have a form fragment called fragment_user_profile_data
where I get user's data, a UpdateProfileForm
class to store it and pass it to an AsyncTask called UpdateProfile
used to update the user information in server side when clicking the saveProfile
button.
However, if a try that line
new UpdateProfile().execute(upf);
an
'com.example.UserProfile.this' cannot be referenced from a static context
error will be shown by Android Studio. I was told either to take the "static" property off from the PlaceholderFragment class or to make my AsyncTask static, but neither of them work.
I'm stuck in this so any help would be appreciated!
So, basically what I was doing was wrong since I was using a single fragment (PlaceholderFragment
) for three separate responsibilities, as @cricket_007 mentioned in his comment to my question.
I did this because the only TabbedActivity tutorial I found uses it.
Basically, when working with a TabbedActivity, you don't need the PlaceholderFragment so you can easily get rid of it. Now, in the getItem
method of the SectionsPagerAdapter
class generated within the activity, replace the following line
return PlaceholderFragment.newInstance(position + 1);
with a switch statement for the position argument and return an instance of the appropriate fragment. i.e.:
public Fragment getItem(int position) {
switch (position) {
case 0:
UserProfileData tab1 = new UserProfileData();
return tab1;
case 1:
UserProfileCollections tab2 = new UserProfileCollections();
return tab2;
case 2:
UserProfileBalance tab3 = new UserProfileBalance();
return tab3;
default:
return null;
}
}
Finally, declare the AsyncTask and the form class into the Fragment class and set the EditTexts' values and button's functionality within the Fragment's onCreateView
method, then inflate it and return it.
I hope this answer helps given the lack of samples about this Activity. Thank you for your answers anyways!