0

私はFirebaseListAdapterとFirebaseRecyclerAdapterと同じ方法でFirebaseを実装しているカスタムPagerAdapterを実装しようとしています。 GitHub hereのソースコードを見ましたが、Android Studioでいくつかのエラーが発生しています。ここで指定したリンクからクラスの多くを利用し、私のFirebasePagerAdapterです:指定されたリンクから使用AndroidでFirebaseを使用してカスタムPagerAdapterを実装する方法は?

public abstract class FirebasePagerAdapter<T> extends PagerAdapter implements FirebaseAdapter, ChangeEventListener { 

    private static String TAG = "firebasepageradapter"; 
    protected final Activity mActivity; 
    protected final ObservableSnapshotArray<T> mSnapshots; 
    protected final int mLayout; 

    /** 
    * @param activity The {@link Activity} containing the {@link android.support.v4.view.ViewPager} 
    * @param modelLayout This is the layout used to represent a single list item. You will be 
    *     responsible for populating an instance of the corresponding view with the 
    *     data from an instance of modelClass. 
    * @param snapshots The data used to populate the adapter 
    */ 
    public FirebasePagerAdapter(Activity activity, 
           ObservableSnapshotArray<T> snapshots, 
           @LayoutRes int modelLayout) { 
     mActivity = activity; 
     mSnapshots = snapshots; 
     mLayout = modelLayout; 

     startListening(); 
    } 

    /** 
    * @param parser a custom {@link SnapshotParser} to convert a {@link DataSnapshot} to the model 
    *    class 
    * @param query The Firebase location to watch for data changes. Can also be a slice of a 
    *    location, using some combination of {@code limit()}, {@code startAt()}, and 
    *    {@code endAt()}. <b>Note, this can also be a {@link DatabaseReference}.</b> 
    * @see #FirebasePagerAdapter(Activity, ObservableSnapshotArray, int) 
    */ 
    public FirebasePagerAdapter(Activity activity, 
           SnapshotParser<T> parser, 
           @LayoutRes int modelLayout, 
           Query query) { 
     this(activity, new FirebaseArray<>(query, parser), modelLayout); 
    } 

    /** 
    * @see #FirebasePagerAdapter(Activity, SnapshotParser, int, Query) 
    */ 
    public FirebasePagerAdapter(Activity activity, 
           Class<T> modelClass, 
           @LayoutRes int modelLayout, 
           Query query) { 
     this(activity, new ClassSnapshotParser<>(modelClass), modelLayout, query); 
    } 

    @Override 
    public int getCount() { 
     return mSnapshots.size(); 
    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     return false; 
    } 

    @Override 
    public void startListening() { 
     if (!mSnapshots.isListening(this)) { 
      mSnapshots.addChangeEventListener(this); 
     } 
    } 

    @Override 
    public void cleanup() { 
     mSnapshots.removeChangeEventListener(this); 
    } 

    @Override 
    public Object getItem(int position) { 
     return mSnapshots.get(position).getKey().hashCode(); 
    } 

    @Override 
    public DatabaseReference getRef(int position) { 
     return mSnapshots.get(position).getRef(); 
    } 

    @Override 
    public void onChildChanged(ChangeEventListener.EventType type, DataSnapshot snapshot, int index, int oldIndex) { 
     notifyDataSetChanged(); 
    } 

    @Override 
    public void onDataChanged() { 

    } 

    @Override 
    public void onCancelled(DatabaseError error) { 
     Log.w(TAG, error.toException()); 
    } 

    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     View view = mActivity.getLayoutInflater().inflate(mLayout, container, false); 

     T model = getItem(position); 

     // Call out to subclass to marshall this model into the provided view 
     populateView(view, model, position); 

     return view; 
    } 

    /** 
    * Each time the data at the given Firebase location changes, 
    * this method will be called for each item that needs to be displayed. 
    * The first two arguments correspond to the mLayout and mModelClass given to the constructor of 
    * this class. The third argument is the item's position in the list. 
    * <p> 
    * Your implementation should populate the view using the data contained in the model. 
    * 
    * @param v  The view to populate 
    * @param model The object containing the data used to populate the view 
    * @param position The position in the list of the view being populated 
    */ 
    protected abstract void populateView(View v, T model, int position); 

} 

クラス:

  • ChangeEventListener
  • ClassSnapshotParser
  • FirebaseAdapter
  • FirebaseArray
  • ObservableSnapshotArray
  • 前提条件
  • SnapshotParser

は、私が激しくGoogleで検索しましたし、SO例や解決のためではなく、FirebasePagerAdapterの必要性にもかかわらず、誰もが解決策を考え出すていないか、少なくともそれが公開されて。私はこれを有効にして、それが有用であるかもしれない他の開発者のために公開したいと思います。私は取得しています

エラーがinstantiateItem方法でT model = getItem(position);である「互換性のない型:必要なT、見つかったオブジェクト」 とthis(activity, new FirebaseArray<>(query, parser), modelLayout);と「引数を推論することはできません」。

私の希望は、これらのエラーを解決することで、アダプタが正常に動作することです。

+0

注:これは他の同様の質問と重複していません。私は他のSOの質問を見てきましたが、どちらも彼らが試したことの例や始める基礎を示していませんでした。この質問は、問題を解決するのに役立つ人にとっては、良い出発点です。 –

答えて

0

getItemメソッドをGenericType public T getItem(int position)に変更してこの問題を解決しました。それは今、完璧に動作します。私はそれが役に立つかもしれない他の誰のためのGitHub上で動作するように、このために必要なすべてのものを掲載している

public abstract class FirebasePagerAdapter<T> extends PagerAdapter implements FirebaseAdapter, ChangeEventListener { 

    private static String TAG = "firebasepageradapter"; 
    protected final Activity mActivity; 
    protected final ObservableSnapshotArray<T> mSnapshots; 
    protected final int mLayout; 

    /** 
    * @param activity The {@link Activity} containing the {@link android.support.v4.view.ViewPager} 
    * @param modelLayout This is the layout used to represent a single list item. You will be 
    *     responsible for populating an instance of the corresponding view with the 
    *     data from an instance of modelClass. 
    * @param snapshots The data used to populate the adapter 
    */ 
    public FirebasePagerAdapter(Activity activity, 
           ObservableSnapshotArray<T> snapshots, 
           @LayoutRes int modelLayout) { 
     mActivity = activity; 
     mSnapshots = snapshots; 
     mLayout = modelLayout; 

     startListening(); 
    } 

    /** 
    * @param parser a custom {@link SnapshotParser} to convert a {@link DataSnapshot} to the model 
    *    class 
    * @param query The Firebase location to watch for data changes. Can also be a slice of a 
    *    location, using some combination of {@code limit()}, {@code startAt()}, and 
    *    {@code endAt()}. <b>Note, this can also be a {@link DatabaseReference}.</b> 
    * @see #FirebasePagerAdapter(Activity, ObservableSnapshotArray, int) 
    */ 
    public FirebasePagerAdapter(Activity activity, 
           SnapshotParser<T> parser, 
           @LayoutRes int modelLayout, 
           Query query) { 
     this(activity, new FirebaseArray<>(query, parser), modelLayout); 
    } 

    /** 
    * @see #FirebasePagerAdapter(Activity, SnapshotParser, int, Query) 
    */ 
    public FirebasePagerAdapter(Activity activity, 
           Class<T> modelClass, 
           @LayoutRes int modelLayout, 
           Query query) { 
     this(activity, new ClassSnapshotParser<>(modelClass), modelLayout, query); 
    } 

    @Override 
    public int getCount() { 
     return mSnapshots.size(); 
    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     return view == object; 
    } 

    @Override 
    public void startListening() { 
     if (!mSnapshots.isListening(this)) { 
      mSnapshots.addChangeEventListener(this); 
     } 
    } 

    @Override 
    public void cleanup() { 
     mSnapshots.removeChangeEventListener(this); 
    } 

    @Override 
    public T getItem(int position) { 
     return mSnapshots.getObject(position); 
    } 

    @Override 
    public DatabaseReference getRef(int position) { 
     return mSnapshots.get(position).getRef(); 
    } 

    @Override 
    public void onChildChanged(ChangeEventListener.EventType type, DataSnapshot snapshot, int index, int oldIndex) { 
     notifyDataSetChanged(); 
    } 

    @Override 
    public void onDataChanged() { 

    } 

    @Override 
    public void onCancelled(DatabaseError error) { 
     Log.w(TAG, error.toException()); 
    } 

    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     View view = mActivity.getLayoutInflater().inflate(mLayout, container, false); 

     //T model = getItem(position); 
     T model = getItem(position); 

     // Call out to subclass to marshall this model into the provided view 
     populateView(view, model, position); 
     container.addView(view); 
     return view; 
    } 

    /** 
    * Each time the data at the given Firebase location changes, 
    * this method will be called for each item that needs to be displayed. 
    * The first two arguments correspond to the mLayout and mModelClass given to the constructor of 
    * this class. The third argument is the item's position in the list. 
    * <p> 
    * Your implementation should populate the view using the data contained in the model. 
    * 
    * @param v  The view to populate 
    * @param model The object containing the data used to populate the view 
    * @param position The position in the list of the view being populated 
    */ 
    protected abstract void populateView(View v, T model, int position); 

    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) { 
     container.removeView((RelativeLayout)object); 
    } 

} 

:ここに私の完全なFirebasePagerAdapterクラスです。あなたはそれを見つけることができますhere

関連する問題