My app uses Fragments in association with a ViewPager to create tabs.
On one tab, I use a ListView to display contents of my database in a table. I have extended SimpleCursorAdapter in order to perform some custom tasks on each row of my list (e.g., alternate row color, buttons, etc.). At one point, I want to click an icon within a row and have it switch tabs, but I am having issues accessing the FragmentManager and ViewPager from within my CustomSimpleCursorAdapter class. I can do this within the fragment itself (see the commented-out section), but when I switched to using the CustomSimpleCursorAdapter, I lost that ability.
So, how can I access my FragmentManager and ViewPager from within my custom class?
note: I have marked the location within CustomSimpleCursorAdapter.java where I am having trouble with
// THIS IS WHERE I AM HAVING ISSUES WITH:
Home.java
package myPackage;
public class Home extends Fragment {
private View rootView;
private CustomSimpleCursorAdapter mySimpleCursorAdapter;
private ViewPager myViewPager;
private SwipeRefreshLayout studentSwipeRefresh;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.home, container, false);
myViewPager = (ViewPager) getActivity().findViewById(R.id.pager);
studentSwipeRefresh = (SwipeRefreshLayout) rootView.findViewById(R.id.student_swipe_refresh);
return rootView;
}
@Override
public void onViewCreated(View rootView, Bundle savedInstanceState) {
super.onViewCreated(rootView, savedInstanceState);
drawTheStudentView();
studentSwipeRefresh.setColorSchemeColors(Color.parseColor(Constants.RED), Color.parseColor(Constants.ORANGE), Color.parseColor(Constants.YELLOW), Color.parseColor(Constants.GREEN), Color.parseColor(Constants.BLUE), Color.parseColor(Constants.INDIGO), Color.parseColor(Constants.VIOLET));
studentSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
studentSwipeRefresh.setRefreshing(false);
drawTheStudentView();
}
});
}
private void drawTheStudentView(){
DatabaseHelper myDBHelper = new DatabaseHelper(getActivity());
Cursor studentCursor = myDBHelper.getStudentsCursor();
String[] fromColumns = {"_id","studentID","location"};
int[] toViews = {R.id.student_number_textview, R.id.student_id_textview, R.id.student_location.textview};
mySimpleCursorAdapter = new CustomSimpleCursorAdapter(getActivity(), R.layout.student_layout, studentCursor, fromColumns, toViews, 0);
// Replace the _id column with a student count
mySimpleCursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
String counter = Integer.toString((cursor.getPosition()+1));
TextView modifiedTextView = (TextView) view;
if(columnIndex == 0){
modifiedTextView.setText(counter);
return true;
}
return false;
}
});
ListView myListView = (ListView) rootView.findViewById(R.id.student_row);
// I COMMENTED THIS OUT. THIS WORKS FOR CLICKING THE ENTIRE ROW AND HAVING AN ACTION PERFORMED.
// Listen for somebody clicking on a Student ID, and process
// myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// @Override
// public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Cursor subCursor = (Cursor) mySimpleCursorAdapter.getItem(position);
// String studentIDNumber = subCursor.getString(subCursor.getColumnIndex("studentID"));
//
// StudentInformation studentInformation = (StudentInformation) getFragmentManager().findFragmentByTag(getFragmentTag(Constants.TAB_INDEX_PATIENT_VITALS));
// studentInformation.setStudendIDNumber(studentIDNumber);
//
// myViewPager.setCurrentItem(Constants.TAB_INDEX_STUDENT_LOCATION);
// }
// });
// Draw the list
myListView.setAdapter(mySimpleCursorAdapter);
myDBHelper.close();
}
// Pass me a tab index (see Constants.java) and I'll return a refrence to that tab.
private String getFragmentTag(int tagID){
return "android:switcher:" + R.id.pager + ":" + tagID;
}
}
CustomSimpleCursorAdapter.java
package myPackage;
public class CustomSimpleCursorAdapter extends SimpleCursorAdapter {
private Context context;
private Cursor cursor;
public CustomSimpleCursorAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int flags) {
super(context, layout, cursor, from, to, flags);
this.context = context;
this.cursor = cursor;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
// Alternate the color of the data rows.
View row = super.getView(position, convertView, parent);
if(position % 2 == 0){
row.setBackgroundColor(Color.parseColor(Constants.WHITE));
} else {
row.setBackgroundColor(Color.parseColor(Constants.LIGHTGREY));
}
// If the "Information" icon/button is clicked, set the information and switch to that tab/fragment.
ImageView statusButton = (ImageView)row.findViewById(R.id.student_status_button);
statusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View myView) {
cursor.moveToPosition(position);
String studentID = cursor.getString(1);
Toast statusToast = Toast.makeText(context, "Showing information for student " + studentID, Toast.LENGTH_SHORT);
statusToast.show();
// THIS IS WHERE I AM HAVING ISSUES WITH:
// * getFragmentManager()
// * myViewPager.setCurrentItem()
StudentInformation studentInformation = (StudentInformation) getFragmentManager().findFragmentByTag(getFragmentTag(Constants.TAB_INDEX_STUDENT_INFORMATION));
studentInformation.setStudentNumber(studentID);
myViewPager.setCurrentItem(Constants.TAB_INDEX_STUDENT_INFORMATION);
}
});
// If the "Location" button is clicked, then show a Toast with the information.
ImageView locationButton = (ImageView)row.findViewById(R.id.student_location_button);
locationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View myView) {
cursor.moveToPosition(position);
String studentID = cursor.getString(1);
Toast statusToast = Toast.makeText(context, "Sending location for student " + studentID + " to MESA", Toast.LENGTH_LONG);
statusToast.show();
}
});
return row;
}
// Pass me a tab index (see Constants.java) and I'll return a refrence to that tab.
private String getFragmentTag(int tagID){
return "android:switcher:" + R.id.pager + ":" + tagID;
}
}
UPDATE:
I tried passing in FragmentManager and ViewPager references into the constructor of my CustomSimpleCursorAdapter, but that didn't seem to work.
1) Create an interface for example:
public interface MyTabChanger{
public void changeTabTo(int nextTabIndex);
}
2) implement that interface with your fragment.
3) inside changeTabTo
call in your fragment you can use below code:
myViewPager.setCurrentItem(nextTabIndex);
4) change the constructor of your Adapter
...
public class CustomSimpleCursorAdapter extends SimpleCursorAdapter {
private Context context;
private Cursor cursor;
private MyTabChanger mCallback;
public CustomSimpleCursorAdapter(MyTabChanger myTabChanger,Context context, int layout, Cursor cursor, String[] from, int[] to, int flags) {
super(context, layout, cursor, from, to, flags);
this.context = context;
this.cursor = cursor;
mCallback = myTabChanger;
}
...
5) then in your adapter when you want to change the viewpager
simply call changeTabTo
. For example:
mCallback.changeTabTo(Constants.TAB_INDEX_STUDENT_INFORMATION);
I think this is the general and cleanest solution you can use. In this way you can use the adapter
with any Fragment
and Activity
as long as they implements MyTabChanger
.