我是java初学者。
我正在使用firestore recyclerview,我想计算其中显示的项目。我的回收站工作得很好。我的最终目标是在我的recyclerview为空时更改布局。我试过用 getItemCount
, onDataChanged
以及 ObservableSnashotArray
. 我读了很多解答,但我的 getItemcount
每次返回0。
这是我的适配器:
public class ArticleAdapter extends FirestoreRecyclerAdapter<Article, ArticleAdapter.ArticleHolder> {
private ArticleAdapter.OnItemClickListener listener;
private FirestoreRecyclerOptions<Article> options;
private ObservableSnapshotArray<Article> mSnapshots;
public ArticleAdapter(FirestoreRecyclerOptions<Article> options) {
super(options);
this.options = options;
mSnapshots = options.getSnapshots();
}
@Override
protected void onBindViewHolder(final ArticleHolder holder, int position, Article model) {
holder.textViewTitle.setText(model.getTitle());
holder.textViewDescription.setText(model.getDescription());
holder.textViewPrice.setText("(" + model.getPrice() + "$)");
}
@NonNull
@Override
public ObservableSnapshotArray<Article> getSnapshots() {
return mSnapshots;
}
@Override
public int getItemCount() {
final int count = options.getSnapshots() != null
? options.getSnapshots().size() : 0;
return count;
}
@NonNull
@Override
public ArticleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.article_item,
parent, false);
return new ArticleAdapter.ArticleHolder(v);
}
class ArticleHolder extends RecyclerView.ViewHolder {
public TextView textViewTitle;
public TextView textViewDescription;
public TextView textViewPrice;
public TextView textViewQuantity;
public TextView textViewEmpty;
public Button mbuttonPlusArticle;
public Button mbuttonminusArticle;
CardView cardView;
public ArticleHolder(final View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.text_view_articletitle);
textViewDescription = itemView.findViewById(R.id.text_view_articledescription);
textViewPrice = itemView.findViewById(R.id.text_view_articleprice);
textViewQuantity = itemView.findViewById(R.id.text_view_articlequantity);
cardView = itemView.findViewById(R.id.mCardView);
mbuttonPlusArticle = itemView.findViewById(R.id.bn_articleplus);
mbuttonminusArticle = itemView.findViewById(R.id.bn_articleminus);
textViewEmpty = itemView.findViewById(R.id.text_view_empty);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION && listener != null) {
listener.onItemClick(getSnapshots().getSnapshot(position), position);
}
}
}
});
cardView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
if (listener != null) {
int position = getAdapterPosition();
contextMenu.setHeaderTitle("Choisir votre Options");
contextMenu.add(getAdapterPosition(), 121, 0, "Modifier");
contextMenu.add(getAdapterPosition(), 122, 1, "Supprimer");
if (position != RecyclerView.NO_POSITION && listener != null) {
listener.onCardViewClick(getSnapshots().getSnapshot(position), position);
}
}
}
});
mbuttonPlusArticle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION && listener != null) {
int count = Integer.parseInt(String.valueOf(textViewQuantity.getText()));
count++;
textViewQuantity.setText("" + count);
int quantity = Integer.parseInt(String.valueOf(textViewQuantity.getText()));
listener.onItemClickPlus(getSnapshots().getSnapshot(position), position, quantity);
}
}
}
});
mbuttonminusArticle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION && listener != null) {
int count = Integer.parseInt(String.valueOf(textViewQuantity.getText()));
if (count == 0) {
} else {
count--;
}
textViewQuantity.setText("" + count);
int quantity = Integer.parseInt(String.valueOf(textViewQuantity.getText()));
listener.onItemClickMinus(getSnapshots().getSnapshot(position), position, quantity);
}
}
}
});
}
}
还有我的片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
String collectionid = getActivity().getIntent().getStringExtra("EVENT_ID");
View view = inflater.inflate(R.layout.fragment_article, container, false);
textViewResultatCommande = view.findViewById(R.id.text_view_resultatcommande);
textViewEmpty = view.findViewById(R.id.text_view_empty);
bn_commande = view.findViewById(R.id.bn_Valider_commande);
bn_commande.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
open_dialog(view);
}
});
setUpRecyclerview(view);
setUpListView(view);
dialog = new Dialog(getActivity());
return view;
}
private void setUpRecyclerview(final View view) {
String collectionid = getActivity().getIntent().getStringExtra("EVENT_ID");
final Query query = articlelistref
.document(collectionid)
.collection("Articlebook")
.orderBy("title", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Article> options = new FirestoreRecyclerOptions.Builder<Article>()
.setQuery(query, Article.class)
.build();
adapter = new ArticleAdapter(options);
final RecyclerView recyclerView = view.findViewById(R.id.recycler_view_historique);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
recyclerView.setAdapter(adapter);
adapter.startListening();
Toast.makeText(getActivity(), "Nombre 2 :" +adapter.getItemCount(), Toast.LENGTH_SHORT).show();
你能帮助我吗?谢谢您
暂无答案!
目前还没有任何答案,快来回答吧!