android studio recyclerView with firestore
-
මගේ firestore database එකේ collection එකක් තියනවා Orders කියලා
තව collection එකක් තියනවා Sellers කියලා ඒ Sellers එක ඇතුලෙ තව තියනව collection එකක් Fruits කියලා
මට ඕනේ මගෙ android application එකේ Activity එකේ තියන RecyclerView එකේ පෙන්නන්න Orders එකේ තියන furits_ID field එකට සමාන Sellers collection එකේ තියන Fruits collection එකේ Document ID එකට ගැලපෙන data සේරම ටික.
කවුරු හරි එකට විදිහක් දන්නවනම් පොඩ්ඩක් කියල දෙනවද?Activity class:
package com.example.freshbucket.Seller; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.helper.ItemTouchHelper; import android.util.Log; import android.widget.Toast; import com.example.freshbucket.Adapter.OrdersSellerRecylerAdapter; import com.example.freshbucket.Model.PlaceOrder; import com.example.freshbucket.R; import com.firebase.ui.firestore.FirestoreRecyclerOptions; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.firestore.CollectionReference; import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.EventListener; import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.FirebaseFirestoreException; import com.google.firebase.firestore.Query; import com.google.firebase.firestore.QuerySnapshot; import javax.annotation.Nullable; import static android.support.constraint.Constraints.TAG; public class SellerGetOrdersActivity extends AppCompatActivity { String fid, pro; private FirebaseAuth mAuth = FirebaseAuth.getInstance(); String user_id = mAuth.getCurrentUser().getUid(); private FirebaseFirestore db = FirebaseFirestore.getInstance(); private CollectionReference orders = db.collection("Orders"); private CollectionReference sellers = db.collection("Sellers/"+user_id+"/Fruits"); private OrdersSellerRecylerAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_seller_get_orders); setupRecyclerView(); } private void setupRecyclerView() { Query query =orders.orderBy("timestamp", Query.Direction.DESCENDING).whereEqualTo("fruit_ID",fid); FirestoreRecyclerOptions<PlaceOrder> options = new FirestoreRecyclerOptions.Builder<PlaceOrder>().setQuery(query, PlaceOrder.class).build(); adapter = new OrdersSellerRecylerAdapter(options); RecyclerView recyclerView = findViewById(R.id.ordersReView); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(adapter); //swipe delete new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT| ItemTouchHelper.RIGHT) { @Override public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) { return false; } @Override public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) { adapter.deleteItem(viewHolder.getAdapterPosition()); } }).attachToRecyclerView(recyclerView); } @Override protected void onStart() { super.onStart(); adapter.startListening(); sellers.addSnapshotListener(new EventListener<QuerySnapshot>() { @Override public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) { if (e != null) { Log.d(TAG, "Error:" + e.getMessage()); } else { for (DocumentSnapshot doc :queryDocumentSnapshots){ fid = doc.getId(); // pro = doc.getString("province"); // fid = doc.getString("fruit_ID"); Toast.makeText(SellerGetOrdersActivity.this, "Register error:" +fid , Toast.LENGTH_SHORT).show(); } } } }); } @Override protected void onStop() { super.onStop(); adapter.stopListening(); } }
Adapter:
package com.example.freshbucket.Adapter; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.TextView; import com.example.freshbucket.Model.PlaceOrder; import com.example.freshbucket.R; import com.firebase.ui.firestore.FirestoreRecyclerAdapter; import com.firebase.ui.firestore.FirestoreRecyclerOptions; public class OrdersSellerRecylerAdapter extends FirestoreRecyclerAdapter<PlaceOrder, OrdersSellerRecylerAdapter.OrdersSellerHolder> { Context context; public OrdersSellerRecylerAdapter(@NonNull FirestoreRecyclerOptions<PlaceOrder> options) { super(options); } @Override protected void onBindViewHolder(@NonNull final OrdersSellerHolder holder, int position, @NonNull final PlaceOrder model) { holder.txtfruitname.setText(model.getName()); holder.txtqun.setText(model.getQun()); holder.txtcusname.setText(model.getCustomer_Name()); holder.txtaddress1.setText(model.getAddressLine1()); holder.txtaddress2.setText(model.getAddressLine2()); holder.txtcity.setText(model.getCity()); } @NonNull @Override public OrdersSellerHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.order_list_item_seller,viewGroup, false); context = viewGroup.getContext(); return new OrdersSellerHolder(v); } class OrdersSellerHolder extends RecyclerView.ViewHolder{ TextView txtfruitname, txtqun, txtcusname, txtaddress1, txtaddress2, txtcity; EditText tctBprice; public OrdersSellerHolder(@NonNull View itemView) { super(itemView); txtfruitname = itemView.findViewById(R.id.fruitnametext); txtqun = itemView.findViewById(R.id.fruitquntext); txtcusname = itemView.findViewById(R.id.cusnametext); txtaddress1 = itemView.findViewById(R.id.addres1text); txtaddress2 = itemView.findViewById(R.id.addres2text); txtcity = itemView.findViewById(R.id.citytext); } } public void deleteItem(int position) { getSnapshots().getSnapshot(position).getReference().delete(); } }
-
@fern help him.