Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. public class FragmentHome extends Fragment {
  2.  
  3.  
  4. private List<ProductPost> product_list;
  5. private RecyclerAdapter productRecyclerAdapter;
  6. FirebaseFirestore firebaseFirestore;
  7. RecyclerView product_list_view;
  8.  
  9. public FragmentHome() {
  10. // Required empty public constructor
  11. }
  12.  
  13. public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup
  14. container,
  15. Bundle savedInstanceState) {
  16.  
  17. //initialize AsynTask class
  18.  
  19. LoadMyData loadMyData = new LoadMyData(getContext());
  20. loadMyData.execute();
  21.  
  22. View view = inflater.inflate(R.layout.fragment_fragment_home,
  23. container, false);
  24.  
  25. //product list for adapters
  26. product_list = new ArrayList<>();
  27. product_list_view = view.findViewById(R.id.recycler1);
  28. productRecyclerAdapter = new RecyclerAdapter(getActivity(), product_list);
  29. product_list_view.setLayoutManager(new
  30. LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
  31. product_list_view.setAdapter(productRecyclerAdapter);
  32. }
  33.  
  34. public class LoadMyData extends AsyncTask<String, String, String>{
  35.  
  36. private Context context;
  37. private ProgressDialog progressDialog;
  38.  
  39. public LoadMyData(Context context){
  40. this.context = context;
  41. }
  42.  
  43. @Override
  44. protected String doInBackground(String... strings) {
  45.  
  46. //Retrieve politics books for recycler1
  47. Query secondQuery = firebaseFirestore.collection("All_Books")
  48. .whereEqualTo("category", "politics")
  49. .orderBy("time", Query.Direction.DESCENDING);
  50.  
  51. secondQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
  52. @Override
  53. public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
  54.  
  55. if (e != null) {
  56. return;
  57. }
  58. if(queryDocumentSnapshots != null && !queryDocumentSnapshots.isEmpty()){
  59.  
  60. for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
  61.  
  62. if (doc.getType() == DocumentChange.Type.ADDED) {
  63.  
  64. String productID = doc.getDocument().getId();
  65. ProductPost productPost = doc.getDocument().toObject(ProductPost.class).withId(productID);
  66. product_list.add(productPost);
  67. productRecyclerAdapter.notifyDataSetChanged();
  68. }
  69. }
  70. }//else statement here...
  71.  
  72. }
  73. });
  74.  
  75. return null;
  76. }
  77.  
  78. @Override
  79. protected void onPostExecute(String s) {
  80. progressDialog.dismiss();
  81. product_list_view.setAdapter(productRecyclerAdapter);
  82. super.onPostExecute(s);
  83. }
  84.  
  85. @Override
  86. protected void onPreExecute() {
  87. progressDialog = new ProgressDialog(context);
  88. progressDialog.setTitle("Loading data...");
  89. progressDialog.show();
  90. super.onPreExecute();
  91. }
  92. }
  93. }
  94.  
  95.  
  96. //getter and setter class
  97. package com.example.ibrahimsahko.yateerenlite;
  98.  
  99. import java.util.Date;
  100.  
  101. public class ProductPost extends ProductID{
  102. private String Name, book_price, book_title, image, category;
  103. private Date time;
  104. private String qty;
  105.  
  106. public ProductPost(){
  107.  
  108. }
  109.  
  110. public ProductPost(String name, String book_price, String book_title, String image, Date time, String category, String qty) {
  111. this.Name = name;
  112. this.book_price = book_price;
  113. this.book_title = book_title;
  114. this.image = image;
  115. this.time = time;
  116. this.category = category;
  117. this.qty = qty;
  118. }
  119.  
  120. public String getCategory() {
  121. return category;
  122. }
  123.  
  124. public void setCategory(String category) {
  125. this.category = category;
  126. }
  127.  
  128.  
  129. public String getName() {
  130. return Name;
  131. }
  132.  
  133. public void setName(String name) {
  134. Name = name;
  135. }
  136.  
  137. public String getBook_price() {
  138. return book_price;
  139. }
  140.  
  141. public void setBook_price(String book_price) {
  142. this.book_price = book_price;
  143. }
  144.  
  145. public String getBook_title() {
  146. return book_title;
  147. }
  148.  
  149. public void setBook_title(String book_title) {
  150. this.book_title = book_title;
  151. }
  152.  
  153. public String getImage() {
  154. return image;
  155. }
  156.  
  157. public void setImage(String image) {
  158. this.image = image;
  159. }
  160. public Date getTime() {
  161. return time;
  162. }
  163.  
  164. public void setTime(Date time) {
  165. this.time = time;
  166. }
  167.  
  168. public String getQty() {
  169. return qty;
  170. }
  171.  
  172. public void setQty(String qty) {
  173. this.qty = qty;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement