gpt4 book ai didi

java - android recyclerview 首次启动时未加载

转载 作者:行者123 更新时间:2023-12-02 05:37:08 24 4
gpt4 key购买 nike

我正在 Android 中使用 firestore。在我的 fragment 中,我有一个 recyclerview ,它从 firestore 获取数据,但在第一次启动应用程序时,recyclerview 是空的,但是当我移动到另一个 fragment 并返回时,它会被加载有数据。

这是我的适配器类

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

private List<ProductPost> product_list;
FirebaseFirestore firebaseFirestore;
FirebaseAuth auth;
private TextView cartCount;
private Context context;
private int previousPosition = 0;
private int itemInCart;

//pass textview and value of item in cart to constructor
public RecyclerAdapter (Context context, List<ProductPost> product_list, TextView cartCount, int itemInCart){

this.cartCount = cartCount;
this.product_list = product_list;
this.itemInCart = itemInCart;
this.context = context;

}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_main, parent, false);
context = parent.getContext();
firebaseFirestore = FirebaseFirestore.getInstance();
auth = FirebaseAuth.getInstance();
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, @SuppressLint("RecyclerView") int position) {


holder.setIsRecyclable(false);

final String productID = product_list.get(position).ProductID;

String image_uri = product_list.get(position).getImage();
holder.retrieveImage(image_uri);

//if user is authenticated, count number of document and attach the size to the view (floating button)
if(auth.getCurrentUser() != null){

final String currentUser = auth.getCurrentUser().getUid();
firebaseFirestore.collection(currentUser).get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
int count = 0;
for (final QueryDocumentSnapshot document : task.getResult()){

count += Integer.parseInt(document.getString("qty"));
cartCount.setText(Integer.toString(count));
}
}
});
}

holder.postImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {

holder.bookProgress.setVisibility(View.VISIBLE);
firebaseFirestore.collection("All_Books").document(productID)
.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
DocumentSnapshot documentSnapshot = task.getResult();
if(documentSnapshot != null){

String display_price = documentSnapshot.getString("book_price");
String display_name = documentSnapshot.getString("Name");
String display_title = documentSnapshot.getString("book_title");
String display_category = documentSnapshot.getString("category");
String display_image = documentSnapshot.getString("image");
String display_store = documentSnapshot.getString("store_name");
String display_description = documentSnapshot.getString("description");

Intent intent = new Intent().setClass(view.getContext(), BookDetails.class);
intent.putExtra("price", display_price);
intent.putExtra("name", display_name);
intent.putExtra("title", display_title);
intent.putExtra("image", display_image);
intent.putExtra("store", display_store);
intent.putExtra("category", display_category);
intent.putExtra("desc", display_description);
intent.putExtra("product_id", productID);
view.getContext().startActivity(intent);
}else{
holder.bookProgress.setVisibility(View.INVISIBLE);
Toast.makeText(context, "This book is finished in the store!", Toast.LENGTH_LONG).show();
}
}else{
String error = Objects.requireNonNull(task.getException()).getMessage();
Toast.makeText(context, error, Toast.LENGTH_LONG).show();
holder.bookProgress.setVisibility(View.INVISIBLE);
}
}
});

}
});
}

@Override
public int getItemCount() {
return product_list.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

private View mview;
private ImageView postImage;
private ProgressBar bookProgress;
TextView mainText;

private ViewHolder(View itemView) {
super(itemView);
mview = itemView;

postImage = mview.findViewById(R.id.product);
bookProgress = mview.findViewById(R.id.bookProgress);
mainText = mview.findViewById(R.id.mainText);
}

private void retrieveImage(String downloadUri){

postImage = mview.findViewById(R.id.product);

Glide.with(context).load(downloadUri).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@android.support.annotation.Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
mainText.setVisibility(View.GONE);
return false;
}

@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
mainText.setVisibility(View.GONE);
return false;
}
})
.into(postImage);
}


}

}

我的 fragment 类

    Query.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

if (e != null) {
return;
}
if(queryDocumentSnapshots != null && !queryDocumentSnapshots.isEmpty()){

for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {

if (doc.getType() == DocumentChange.Type.ADDED) {

String productID = doc.getDocument().getId();
ProductPost productPost = doc.getDocument().toObject(ProductPost.class).withId(productID);
product_list.add(productPost);
productRecyclerAdapter.notifyDataSetChanged();
}
}
}

我希望 fragment 在首次启动时按预期加载。需要帮助...提前致谢。

最佳答案

您第一次将适配器设置在哪里?因为你应该在列表中获取元素后执行此操作

                if (doc.getType() == DocumentChange.Type.ADDED) {

String productID = doc.getDocument().getId();
ProductPost productPost = doc.getDocument().toObject(ProductPost.class).withId(productID);
product_list.add(productPost);
//set your adapter here first
productRecyclerAdapter.notifyDataSetChanged();
}

关于java - android recyclerview 首次启动时未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56158544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com