I'm building an App which extracts data from a SQLite local database.
I have an Activity, in which I use a ViewPager; in this ViewPager, there are 12 swipable Fragments.
I'd like to implement a "Reload" button which allows user to reload the actual Fragments, removing any changes caused by user modifications and resuming the original state of Fragment.
I tried to use notifyDataSetChanged()
method on myAdapter (an extension of FragmentStatePagerAdapter
) even if it should reload the whole pager: this method works (each Fragment is reloaded), but user modifications are not removed (for example, if I get an EditText and if I replace its text, pushing "Reload" button doesn't resume its original state).
I also tried to replace getItemPosition()
method as suggested, but it doesn't solve the problem.
1) Activity:
public class MyActivity extends AppCompatActivity {
//code
ViewPager pager;
MyAdapter adapter;
TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
//Pager settings
adapter = new Adapter(getSupportFragmentManager(),this);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
//TabLayout settings
tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(pager);
//...
}
//...
//Method called after clicking on 'Reload' button
public void reloadFragment() {
adapter.notifyDataSetChanged();
}
}
2) Adapter:
public class Adapter extends FragmentStatePagerAdapter {
//...
public Fragment getItem(int position) {
Fragment f = null;
switch (position) {
case 0:
f = new MyFragment01();
break;
//case from 0 to 11
}
return f;
}
//...
@Override
public int getItemPosition( Object object ) {
return POSITION_NONE;
}
3) MyFragment01 (1 of 12):
import android.support.v4.app.Fragment;
//...
public class MyFragment01 extends Fragment {
PopulateEditText editText01;
public MyFragment01() {
//Empty constructor used to load from MyActivity
}
@Override
public void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//...
View myFragmentView = inflater.inflate(R.layout.layout_fragment_01, container, false);
//...
acquire(myFragmentView); //Acquire layout elements (EditTexts, Spinners, other)
populate(); //Insert text on previous layout elements, based on SQLite data
//...
//'Reload' button
ImageButton test = (ImageButton) myFragmentView.findViewById(R.id.testBtn);
test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MyActivity)getActivity()).reloadFragment();
}
});
}
}
}
EDIT #1 (about user yshahak's question)
3b) Details of method acquire(myFragmentView);
from MyFragment01
, which is called to inflate elements:
public void acquire(View myFragmentView) {
editText01 = new PopulateEditText(R.id.editText01,myFragmentView);
//...
}
public void populate() {
//cursor is a cursor which extracts a record from SQLite Database
//column is an attribute of this record
editText01.initializeEditText(cursor,"column",true);
//...
}
4) PopulateEditText class:
public class PopulateEditText {
EditText editText;
Activity activity;
//Constructor
public PopulateEditText(int elementFromLayout, View myFragmentView) {
this.editText=(EditText) myFragmentView.findViewById(elementFromLayout);
reset();
}
public void reset() {
if(editText!=null)
editText.setText("");
}
public void initializeEditText(Cursor c, String column, boolean enableEditText) {
this.column=column;
String s = c.getString(c.getColumnIndex(column));
editText.setText(s);
editText.setSelection(editText.getText().length());
editText.setEnabled(enableEditText);
}
}
EDIT #2
When i press 'Reload' button, these methods are called from MyFragment01
(checked by some breakpoints on debug):
1. onCreateView
2. acquire(myFragmentView)
3. populate()
And if I check my EditText's text, it's correctly reloaded at the start value, so it seems that "new Fragment" is reloaded, but not attached to ViewPager
element.
Example:
- Start value: Text
- Modified value: Text 1234
- Hitting 'Reload', actual value: Text 1234
- getText().toString() from EditText: Text (expected result, not shown on actual view)