Мое требование - добавить сведения об элементе в новую коллекцию, когда я нажимаю кнопку добавления внутри элемента RecyclerView
.
В настоящее время я получаю данные из коллекции Firestore в RecyclerView
, теперь Мне нужно добавить данные внутри элемента в новую коллекцию, используя тот же идентификатор документа, когда я нажимаю кнопку добавления.
Любой, пожалуйста, помогите мне разобраться.
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*/
}