FirebaseRecyclerAdapter multiple viewHolder

我试图使用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();

}

};

我希望我没有错过了一些东西,这清除所有的疑虑。但如果它不,我会很乐意添加更多的信息。

回答:

好的。我实现了我自己的自定义实现RecyclerAdapter而不是FirebaseRecyclerAdapter。只有意识到它并没有奏效。但真正的问题在于RecyclerView因为上面的textView的layout_height设置为match_parent而不可见。所以这一直是我愚蠢的错误。

以上是 FirebaseRecyclerAdapter multiple viewHolder 的全部内容, 来源链接: utcz.com/qa/265053.html

回到顶部