Reputation: 205
So, I am back to trying to update the EditText content in a Fragment from the Activity. I should be getting the right fragment object from within the viewpager using its position, however the method called, drawSurname(surname), is still giving me 'Cannot resolve' error.
Relevant code from FormActivity;
//initialise pager for swipe screens
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
public void DrawText() {
// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();
if (user.size() != 0) {
Log.e(TAG, "string surname: " + surname);
// Displaying the user details on the screen
Fragment n = adapter.getRegisteredFragment(0);
n.drawSurname(surname);
}else{
Log.e(TAG, "table empty");
}
}
public class MyPagerAdapter extends FragmentStatePagerAdapter {
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return FirstFragment.newInstance("@string/form_instruct");
case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
case 3: return FourthFragment.newInstance("FourthFragment, Instance 1");
//case 4: return FifthFragment.newInstance("ThirdFragment, Instance 3");
default: return FirstFragment.newInstance("FirstFragment, Default");
}
}
@Override
public int getCount() {
return 4;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}
FirstFragment.java
public class FirstFragment extends android.support.v4.app.Fragment {
//create variables & Logcat tag
private static final String TAG = FirstFragment.class.getSimpleName();
private EditText inputTitle;
private EditText inputName;
private EditText inputSurname;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_first, container, false);
//set inputs for fields
inputTitle = (EditText) v.findViewById(R.id.titleText);
inputName = (EditText) v.findViewById(R.id.foreText);
inputSurname = (EditText) v.findViewById(R.id.surnameText);
TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
tv.setText(getArguments().getString("msg"));
inputSurname = (EditText) getActivity().findViewById(R.id.surnameText);
return v;
}
public static FirstFragment newInstance(String text) {
FirstFragment f = new FirstFragment(text);
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
public void drawSurname(String x) {
inputSurname.setText(x);
}
}
I was sure I'd got the fragment correctly with getRegisteredFragment(), but cannot see why the method wouldn't call otherwise.
Upvotes: 1
Views: 446
Reputation:
The problem comes from the fact that Fragment class is not supposed to have a function called drawSurname but FirstFragment is. To solve this create an abstract class extending Fragment called SuperFrag from which all your custom fragments will inherit. In SuperFrag add a function called drawSurname. You can override it in your inheriting classes. Finally SparseArray should be a list of SuperFrag
Upvotes: 4