如何从SimpleCrsorAdapter的子类中操纵我的Android片段?
我的应用程序使用与ViewPager关联的片段来创建选项卡 在一个选项卡上,我使用ListView在表中显示数据库的内容。我扩展了SimpleCursorAdapter,以便在列表的每一行上执行一些自定义任务(例如,替换行颜色、按钮等)。有一次,我想单击一行中的图标并让它切换选项卡,但从CustomSimpleCorsOrAdapter类中访问FragmentManager和ViewPager时遇到问题。我可以在片段本身中实现这一点(参见注释掉的部分),但是当我切换到使用CustomSimpleCursorAdapter时,我失去了这种能力 那么,如何从自定义类中访问FragmentManager和ViewPager 注意:我已经在CustomSimpleCursorAdapter.java中标记了我遇到问题的位置如何从SimpleCrsorAdapter的子类中操纵我的Android片段?,android,android-fragments,simplecursoradapter,Android,Android Fragments,Simplecursoradapter,我的应用程序使用与ViewPager关联的片段来创建选项卡 在一个选项卡上,我使用ListView在表中显示数据库的内容。我扩展了SimpleCursorAdapter,以便在列表的每一行上执行一些自定义任务(例如,替换行颜色、按钮等)。有一次,我想单击一行中的图标并让它切换选项卡,但从CustomSimpleCorsOrAdapter类中访问FragmentManager和ViewPager时遇到问题。我可以在片段本身中实现这一点(参见注释掉的部分),但是当我切换到使用CustomSimple
// 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;
}
}
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;
}
}
更新:
我尝试将FragmentManager和ViewPager引用传递到CustomSimpleCursorAdapter的构造函数中,但这似乎不起作用。您可以将初始化适配器的上下文强制转换为活动(前提是将活动用作上下文) 希望这能让您很好地了解需要做什么:)1)创建一个界面,例如:
public interface MyTabChanger{
public void changeTabTo(int nextTabIndex);
}
mCallback.changeTabTo(Constants.TAB_INDEX_STUDENT_INFORMATION);
2)使用片段实现该接口
3)在changeTabTo
内调用片段,您可以使用以下代码:
myViewPager.setCurrentItem(nextTabIndex);
4)更改适配器的构造函数
...
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)然后在您的适配器中,当您想要更改viewpager
时,只需调用changeTabTo
。例如:
public interface MyTabChanger{
public void changeTabTo(int nextTabIndex);
}
mCallback.changeTabTo(Constants.TAB_INDEX_STUDENT_INFORMATION);
我认为这是您可以使用的最通用、最干净的解决方案。通过这种方式,您可以将
适配器
用于任何片段
和活动
,只要它们实现了MyTabChanger
您是在多个活动中使用此游标适配器,还是仅在一个活动中使用此游标适配器?@TerryWhite仅在一个活动中使用此游标适配器。只需在适配器的构造函数中传递对片段的引用。然后您可以访问ViewPager和FragmentManager,当然您需要在片段中创建一些getter方法,因为这些引用是片段的私有引用。@Luksprog您是指上下文吗?因为我尝试将其传入,但没有任何帮助。创建适配器时,请执行以下操作:新建CustomSimpleCursorAdapter(this,R.layout.student\u布局,studentCursor,fromColumns,toViews,0);。然后在适配器的构造函数中,还有片段和上下文:public customsimplecursorsadapter(Home frag,int layout,Cursor Cursor,String[]from,int[]to,int flags){super(frag.getActivity(),layout,Cursor,from,to,flags);this.Context=frag.getActivity();//frag.getViewPager()//frag.getActivity().getSupportFragmentManager()因此,我完全按照您的建议进行了操作,在myViewPager上得到了一个“无法解析符号”。这对于更改选项卡非常有效,但我还需要访问FragmentManager以向该片段传递信息:StudentInformation StudentInformation=(StudentInformation)getFragmentManager().findFragmentByTag(getFragmentTag(Constants.TAB_INDEX_STUDENT_INFORMATION))和studentInformation.setStudentNumber(studentID);这是一个。你可以用这些想法来解决很多问题。你必须在你的情况下认识到它们。我相信你也可以用上面的模式来管理碎片管理器。所以,你是说没有办法做我想让它做的事?我不能在“我的主页”选项卡中单击列表视图一行内的按钮,让它执行这两个功能ns?那似乎不对。不,我不是那个意思,我的意思是为了使用片段,你也可以使用上面的想法。创建接口并用你的片段实现它。然后……我要你做的就是首先理解整个模式,然后根据你的情况使用。你肯定会用这个模式来处理你的片段。