gpt4 book ai didi

java - 第一次启动应用程序时,recyclerview 为空

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

我有这个问题,我正在尝试解决但没有成功。我创建了一个应用程序,显示主要 Activity 的回收 View 。但是,当应用程序第一次启动时,recyclerview 是空的,尽管我正在从 firestore 获取数据,但是当我进入另一个 Activity 并返回时,它会被加载。我正在使用 AsyncTask 这是我的代码

public class FragmentHome extends Fragment {


private List<ProductPost> product_list;
private RecyclerAdapter productRecyclerAdapter;
FirebaseFirestore firebaseFirestore;
RecyclerView product_list_view;

public FragmentHome() {
// Required empty public constructor
}

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup
container,
Bundle savedInstanceState) {

//initialize AsynTask class

LoadMyData loadMyData = new LoadMyData(getContext());
loadMyData.execute();

View view = inflater.inflate(R.layout.fragment_fragment_home,
container, false);

//product list for adapters
product_list = new ArrayList<>();
product_list_view = view.findViewById(R.id.recycler1);
productRecyclerAdapter = new RecyclerAdapter(getActivity(), product_list);
product_list_view.setLayoutManager(new
LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
product_list_view.setAdapter(productRecyclerAdapter);
}

public class LoadMyData extends AsyncTask<String, String, String>{

private Context context;
private ProgressDialog progressDialog;

public LoadMyData(Context context){
this.context = context;
}

@Override
protected String doInBackground(String... strings) {

//Retrieve politics books for recycler1
Query secondQuery = firebaseFirestore.collection("All_Books")
.whereEqualTo("category", "politics")
.orderBy("time", Query.Direction.DESCENDING);

secondQuery.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();
}
}
}//else statement here...

}
});

return null;
}

@Override
protected void onPostExecute(String s) {
progressDialog.dismiss();
product_list_view.setAdapter(productRecyclerAdapter);
super.onPostExecute(s);
}

@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(context);
progressDialog.setTitle("Loading data...");
progressDialog.show();
super.onPreExecute();
}
}
}


//getter and setter class
package com.example.ibrahimsahko.yateerenlite;

import java.util.Date;

public class ProductPost extends ProductID{
private String Name, book_price, book_title, image, category;
private Date time;
private String qty;

public ProductPost(){

}

public ProductPost(String name, String book_price, String book_title, String image, Date time, String category, String qty) {
this.Name = name;
this.book_price = book_price;
this.book_title = book_title;
this.image = image;
this.time = time;
this.category = category;
this.qty = qty;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}


public String getName() {
return Name;
}

public void setName(String name) {
Name = name;
}

public String getBook_price() {
return book_price;
}

public void setBook_price(String book_price) {
this.book_price = book_price;
}

public String getBook_title() {
return book_title;
}

public void setBook_title(String book_title) {
this.book_title = book_title;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}
public Date getTime() {
return time;
}

public void setTime(Date time) {
this.time = time;
}

public String getQty() {
return qty;
}

public void setQty(String qty) {
this.qty = qty;
}

}

如果有人能帮我找出 m 代码的问题,我将不胜感激

最佳答案

当您已经在 onCreateView 上拥有适配器时,您可以在再次传递适配器后进行

@Override
protected void onPostExecute(String s) {
progressDialog.dismiss();
//product_list_view.setAdapter(productRecyclerAdapter);
super.onPostExecute(s);
}

关于java - 第一次启动应用程序时,recyclerview 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56194670/

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