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

标签 java android

我有这个问题,我正在尝试解决但没有成功。我创建了一个应用程序,显示主要 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/

相关文章:

Android使用OnPreDrawListener获取imageview的宽度和高度

java - 如何获取 PCollection<String, String> 中的元素总数

Java bean 验证正则表达式摆脱 CSV 注入(inject)

java - 安卓 : Several activities sharing common code

java - 读取蓝牙设备时出现空指针异常

android - 当适配器为空时,有没有办法指定用于 ListView 的布局?

java - java Socket不能接收太多数据

java - 使用 Bouncy CaSTLe (Java) 重建 ED25519 key

java - buildroot 与 jamvm 2.0 for java 8

Android - 选择文件