2016-03-21 16 views
0

私のアプリはViewPagerに関連してFragmentsを使用してタブを作成します。SimpleCursorAdapterのサブクラスからAndroidフラグメントを操作するにはどうすればよいですか?

1つのタブで、私はListViewを使用して、自分のデータベースの内容を表に表示します。私はSimpleCursorAdapterを拡張して、自分のリストの各行(例えば、交互の行の色、ボタンなど)でカスタムタスクを実行しました。ある時点で、行内のアイコンをクリックしてタブを切り替えたいのですが、CustomSimpleCursorAdapterクラス内からFragmentManagerとViewPagerにアクセスする際に問題が発生しています。私はフラグメント自体でこれを行うことができます(コメントアウトされたセクションを参照)。しかし、私がCustomSimpleCursorAdapterを使用するように切り替えたとき、私はその能力を失いました。

私のカスタムクラス内からFragmentManagerとViewPagerにアクセスするにはどうすればよいですか?

注:私は

// THIS IS WHERE I AM HAVING ISSUES WITH: 

とのトラブルを抱えています、私はCustomSimpleCursorAdapter.java内の位置をマークしている 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:

私は私のCustomSimpleCursorAdapterのコンストラクタにFragmentManagerとViewPagerの参照を渡してみましたが、それは動作していないようでした。

あなたは(あなたがコンテキストとしての活動を使用して)あなたがあなたの活動をして、アダプタを初期化コンテキストをキャスト使用することができます
+0

このカーソル・アダプターは、複数のアクティビティーで使用しますか? –

+0

@TerryWhiteちょうど1つのアクティビティ。 – Brian

+0

アダプタのコンストラクタ内のフラグメントへの参照を渡すだけです。その後、ViewPagerとFragmentManagerにアクセスすることができます。もちろん、それらの参照がフラグメントに対してプライベートであるため、フラグメント内にいくつかのゲッターメソッドを作成する必要があります。 – Luksprog

答えて

1

1)例えばインタフェースの作成:

public interface MyTabChanger{ 

    public void changeTabTo(int nextTabIndex); 
} 

2)あなたのフラグメントとそのインタフェースを実装します。で

... 

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)その後、あなたのアダプタのコンストラクタを変更

myViewPager.setCurrentItem(nextTabIndex); 

4):

3)あなたのフラグメントでchangeTabToコールの内側に、あなたは、コードの下に使用することができますviewpagerを変更する場合は、アダプタをchangeTabToと呼んでください。例:

mCallback.changeTabTo(Constants.TAB_INDEX_STUDENT_INFORMATION); 

これは一般的でクリーンなソリューションです。このようにしてadapterFragmentActivityと使用することができます(MyTabChangerを実装している場合)。

+0

タブを変更するためにはうまくいきましたが、そのフラグメントに情報を渡すためにFragmentManagerにアクセスする必要があります:StudentInformation studentInformation =(StudentInformation)getFragmentManager()。findFragmentByTag(getFragmentTag(Constants.TAB_INDEX_STUDENT_INFORMATION));およびstudentInformation.setStudentNumber(studentID); – Brian

+0

これは[デザインパターン](https://en.wikipedia.org/wiki/Software_design_pattern)です。あなたは多くの問題を解決するためにアイデアを使用することができます。あなたの状況でそれらを認識する必要があります。私はあなたが 'FragmentManager'のために上記のパターンを使うこともできると確信しています –

+0

だから、私がしたいことをする方法がないと言っていますか?ホームタブのListViewの行内にあるボタンをクリックすることができず、これらの2つの機能を実行できますか?それは正しいとは思わない。 – Brian

-1

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; 


    //Where you need to... 
    ((MyActivity)context).mViewPager.setCurrentItem() 
} 

うまくいけば、これはあなたが何をする必要があるかの良いアイデアを提供します:)

+0

私はあなたが提案したとおりに正確に行い、myViewPagerで「シンボルを解決できません」というメッセージが表示されます。 – Brian