Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement