首次启动应用程序时,recyclerview为空

时间:2019-05-17 23:20:36

标签: java android

我有一个我要解决的问题,但没有成功。我创建了一个显示主要活动的recyclerview的pp。但是,当我第一次启动该应用程序时,虽然我正在从Firestore中获取数据,但recyclerview是空的,但是当我进行另一项活动并回来时,它会被加载。我正在使用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代码的问题,我将不胜感激

1 个答案:

答案 0 :(得分:0)

当您已经在onCreateView上使用适配器时,您可以在发布时再次传递适配器

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