0

My requirement is to add the item details in a new collection when I click on the add button inside the RecyclerView item.

Currently I am retrieving data from Firestore collection into the RecyclerView, now I need to add the data inside the item to the new collect using the same document ID when I click on add button.

Any one please help me to sort it out.

GroceryCat

public class GroceryCat extends AppCompatActivity {

    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private GroceryCatAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grocerycat);
        setUpRecyclerView();
    }

    private void setUpRecyclerView() {

        // create the get Intent object
        Intent intent = getIntent();

        // receive the value by getStringExtra() method
        // and key must be same which is send by first activity
        String str = intent.getStringExtra("documentID");
        Toast.makeText(GroceryCat.this," ID: " + str, Toast.LENGTH_SHORT).show();

        CollectionReference productsRef = db.collection("Inventory").document(str).collection("Productslist");

        Query query = productsRef.orderBy("name", Query.Direction.DESCENDING);

        FirestoreRecyclerOptions<GroceryCatModel> options = new FirestoreRecyclerOptions.Builder<GroceryCatModel>().setQuery(query, GroceryCatModel.class).build();

        adapter = new GroceryCatAdapter(options);

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

       // Onclick for item in the list

        adapter.setOnItemClickListener(new GroceryCatAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
                GroceryStoresModel groceryStoresModel = documentSnapshot.toObject(GroceryStoresModel.class);
                String id = documentSnapshot.getId();
                String path = documentSnapshot.getReference().getPath();

                //Toast displaying the document id

                Toast.makeText(GroceryCat.this,
                        "Position: " + position + " ID: " + id, Toast.LENGTH_SHORT).show();

            }
        });
    }

}

GroceryCatAdapter

public class GroceryCatAdapter extends FirestoreRecyclerAdapter<GroceryCatModel, GroceryCatAdapter.NoteHolder> {

    private GroceryCatAdapter.OnItemClickListener listener;
    private FirebaseFirestore db = FirebaseFirestore.getInstance();

    GroceryCatAdapter(@NonNull FirestoreRecyclerOptions<GroceryCatModel> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull GroceryCatAdapter.NoteHolder holder, int position, @NonNull GroceryCatModel model) {
        holder.textViewName.setText(model.getName());
        holder.textViewQuantity.setText(model.getQuantity());
        holder.textViewCost.setText(String.valueOf("Rs"+ model.getCost())  );
    }

    @NonNull
    @Override
    public GroceryCatAdapter.NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.grocerycatrow_item, parent, false);
        return new GroceryCatAdapter.NoteHolder(v);
    }

    class NoteHolder extends RecyclerView.ViewHolder {
        TextView textViewName;
        TextView textViewQuantity;
        TextView textViewCost;
        Button btn;

        NoteHolder(View itemView) {
            super(itemView);
            textViewName = itemView.findViewById(R.id.text_view_name);
            textViewQuantity = itemView.findViewById(R.id.text_view_quantity);
            textViewCost = itemView.findViewById(R.id.text_view_cost);
            btn = itemView.findViewById(R.id.btn);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION && listener != null) {
                        listener.onItemClick(getSnapshots().getSnapshot(position), position);
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(DocumentSnapshot documentSnapshot, int position);
    }

    public void setOnItemClickListener(GroceryCatAdapter.OnItemClickListener listener) {
        this.listener = listener;
    }
}

GroceryCatModel

public class GroceryCatModel {
    private String name;
    private String quantity;
    private int cost;
    
    /*Constructors getters and setters*/
}
Community
  • 1
  • 1
  • Where is your code? – Pratik Butani Mar 05 '20 at 09:44
  • You can check **[this](https://stackoverflow.com/questions/49277797/how-to-display-data-from-firestore-in-a-recyclerview-with-android/49277842)** out. – Alex Mamo Mar 05 '20 at 09:50
  • Hi Pratik Butani, I will edit my question with code. please give me the instructions to do it. – NaNi Prasanna Mar 05 '20 at 10:33
  • As Pratik Butani has rightfully asked, we need to see your code to better help you with your query –  Mar 05 '20 at 10:50
  • please check the code – NaNi Prasanna Mar 05 '20 at 11:23
  • I just want to know that how to perform an action for the button in item of Firestore recycler view. when click on the button add in the recycler view item then in background it has to create a document in new collection and add all the details of the item in the document. – NaNi Prasanna Mar 05 '20 at 11:32
  • I checked your code and I found it a bit confusing, but, from what I understood, you want to add a new subcollection with the data at your `GroceryCatModel()` class every time the button is clicked (which is the `onItemClick()` nested method of `setUpRecyclerView()`), correct? If that is the case, you can check this [documentation](https://firebase.google.com/docs/firestore/manage-data/add-data#add_a_document), on how to add data, with a android example that can be useful. – Ralemos Mar 05 '20 at 17:12
  • I have already did this, but its not working. Please give me an example – NaNi Prasanna Mar 06 '20 at 05:04

1 Answers1

0

As I added to the comments of your question, you could check this documentation for more details and examples on how to add data to firestore, based on what I understood of you code an example that could work is the following:

 public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
     //your already exiting code here

     Map<String, Object> data = new HashMap<>();
     data.put("name", groceryStoresModel.getName());
     data.put("quantity", groceryStoresModel.getQuantity());
     data.put("cost", groceryStoresModel.getCost());

     db.collection(/*your collection name here as a string*/).add(data);

}

NOTE: This is untested so you may have to adapt it.

Ralemos
  • 5,571
  • 2
  • 9
  • 18