2016-08-02 59 views
1

我試圖使用FirebaseRecyclerAdapterRecyclerView來顯示不同類型的項目。特別是用戶對特定股票的股票交易。按照我的理解,我已經擴展了FirebaseRecyclerAdapter,但它不起作用。以下是代碼:FirebaseRecyclerAdapter multiple viewHolder

public abstract class StockTransactionFirebaseRecyclerAdapter extends 
     FirebaseRecyclerAdapter<StockTransactionType, TransactionViewHolder> 
     implements ListenerInterface 
{ 
    private static final String TAG = "StockTxnAdapter"; 
    private DatabaseReference mFirebaseDatabaseReference; 
    private FirebaseListenerManager mFirebaseListenerManager; 
    private String mUserId; 

    public StockTransactionFirebaseRecyclerAdapter (Query ref, DatabaseReference databaseReference, 
                String userId) 
    { 
     // The layout portfolio_buy_transaction_item is just a dummy since we are 
     // inflating our own layouts when required. 
     super (StockTransactionType.class, R.layout.portfolio_buy_transaction_item, 
       TransactionViewHolder.class, ref); 

     mFirebaseDatabaseReference = databaseReference; 
     mFirebaseListenerManager = new FirebaseListenerManager(); 
     mUserId = userId; 
     Log.e (TAG, "Inside StockTransactionFirebaseRecyclerAdapter"); 
    } 

    @Override 
    public int getItemViewType (int position) 
    { 
     StockTransactionType stockTransactionType = getItem (position); 
     Log.e (TAG, "Inside getItemViewType (" + position + "): " + stockTransactionType.returnType()); 
     return stockTransactionType.returnType(); 
    } 

    @Override 
    public TransactionViewHolder onCreateViewHolder (ViewGroup parent, int viewType) 
    { 
     Log.e (TAG, "onCreateViewHolder for " + viewType); 
     View itemView; 
     switch (viewType) 
     { 
      case StockTransactionType.TRANSACTION_TYPE_BUY: 
       itemView = LayoutInflater.from (parent.getContext()) 
         .inflate (R.layout.portfolio_buy_transaction_item, parent, false); 
       return new BuyTransactionViewHolder (itemView); 
      case StockTransactionType.TRANSACTION_TYPE_SELL: 
       itemView = LayoutInflater.from (parent.getContext()) 
         .inflate (R.layout.portfolio_sell_transaction_item, parent, false); 
       return new SellTransactionViewHolder (itemView); 
      case StockTransactionType.TRANSACTION_TYPE_DIVIDEND: 
       itemView = LayoutInflater.from (parent.getContext()) 
         .inflate (R.layout.portfolio_dividend_transaction_item, parent, false); 
       return new DividendTransactionViewHolder (itemView); 
      case StockTransactionType.TRANSACTION_TYPE_SPLIT: 
       itemView = LayoutInflater.from (parent.getContext()) 
         .inflate (R.layout.portfolio_split_transaction_item, parent, false); 
       return new SplitTransactionViewHolder (itemView); 
     } 

     throw new IllegalStateException ("Invalid transaction type: " + viewType); 
    } 

    @Override 
    protected void populateViewHolder (final TransactionViewHolder viewHolder, 
             StockTransactionType stockTransactionType, int position) 
    { 
     final String transactionId = this.getRef (position).getKey(); 
     final Class transaction; 
     final String location; 

     Log.e (TAG, "populateViewHolder: " + stockTransactionType.getClass().getName() + " " 
       + position); 

     switch (stockTransactionType.returnType()) 
     { 
      case StockTransactionType.TRANSACTION_TYPE_BUY: 
       transaction = BuyStockTransaction.class; 
       location = FirebaseDatabaseUtil.CHILD_TRANSACTIONS_BUY; 
       break; 
      case StockTransactionType.TRANSACTION_TYPE_SELL: 
       transaction = SellStockTransaction.class; 
       location = FirebaseDatabaseUtil.CHILD_TRANSACTIONS_SELL; 
       break; 
      case StockTransactionType.TRANSACTION_TYPE_SPLIT: 
       transaction = SplitStockTransaction.class; 
       location = FirebaseDatabaseUtil.CHILD_TRANSACTIONS_SPLIT; 
       break; 
      case StockTransactionType.TRANSACTION_TYPE_DIVIDEND: 
       transaction = DividendStockTransaction.class; 
       location = FirebaseDatabaseUtil.CHILD_TRANSACTIONS_DIVIDEND; 
       break; 
      default: 
       throw new IllegalStateException ("Invalid transaction type: " 
         + stockTransactionType.renameTypeString()); 
     } 

     DatabaseReference databaseReference = mFirebaseDatabaseReference 
       .child (location).child (mUserId).child (transactionId); 
     ValueEventListener valueEventListener = databaseReference 
       .addValueEventListener (new ValueEventListener() 
       { 
        @Override 
        public void onDataChange (DataSnapshot dataSnapshot) 
        { 
         StockTransaction stockTransaction = (StockTransaction) dataSnapshot.getValue (transaction); 
         viewHolder.assignViewHolder (stockTransaction); 
        } 

        @Override 
        public void onCancelled (DatabaseError databaseError) 
        { 
         if (getActivity() instanceof MainActivity) 
         { 
          MainActivity mainActivity = (MainActivity) getActivity(); 
          mainActivity.showDatabaseError (TAG, Thread.currentThread() 
            .getStackTrace(), databaseError); 
         } 
        } 
       }); 

     mFirebaseListenerManager.addListner (databaseReference, valueEventListener); 
    } 

    protected abstract Activity getActivity(); 

    @Override 
    public void removeAllListeners() 
    { 
     mFirebaseListenerManager.removeAllListners(); 
    } 
} 

BuyTransactionViewHolderSellTransactionViewHolder等都是TransactionViewHolder子類,並且,BuyStockTransactionSellStockTransaction等都是StockTransaction子類。

除了構造函數,甚至沒有函數被調用。由於我對android編程新手可能會做些傻事。有人能幫助我理解我做錯了什麼嗎?

編輯

這是怎麼了初始化適配器:

final DatabaseReference databaseReference = mFirebaseDatabaseReference 
      .child (FirebaseDatabaseUtil.CHILD_USER_STOCK_TRANSACTIONS) 
      .child (mUserId).child (mStockId); 
mStockTransactionFirebaseRecyclerAdapter = new StockTransactionFirebaseRecyclerAdapter (
     databaseReference, mFirebaseDatabaseReference, mUserId) 
{ 
    @Override 
    protected Activity getActivity() 
    { 
     return StockDisplayFragment.this.getActivity(); 
    } 
}; 

我希望我沒有錯過了一些東西,這清除所有的疑慮。但如果它不,我會很樂意添加更多的信息。

+0

請附上您的json db streucture和規則,以便我們可以看到這些交易如何存儲 – jirungaray

+0

@jirungaray我已添加您請求的詳細信息。謝謝。 – Prabhat

回答

0

好的。我實現了我自己的自定義實現RecyclerAdapter而不是FirebaseRecyclerAdapter。只有意識到它並沒有奏效。但真正的問題在於RecyclerView因爲上面的textView的layout_height設置爲match_parent而不可見。所以這一直是我愚蠢的錯誤。