I am using a recyclerView to show a list of receipts from Firestore which i have completed and can be seen here (https://i.sstatic.net/tuGQ4.png). Now i want to tap on the lists and be brought to a new activity that contains more detail about that receipt. i followed an online video and currently when you click on a list it shows the position of the item in firestore. I want to click on any receipt in the card view and be brought to a new activity that displays more details about that receipt. As you can see in my firebase and the list view, im only pulling 'store', 'total' and date from firestore but in this new activity you will see all the fields related to that receipt.
I have found similar post about this on stack but i still can't get it to work.
I have been at this for days so any help is greatly appreciated.
ReceiptActivity.java
public class ReceiptActivity extends AppCompatActivity {
private RecyclerView mFirestoreList;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private ReceiptAdapter adapter;
private CollectionReference receiptRef = db.collection( "users" )
.document( "BtliRhtVFiQntlH2Hp1gvjG59b32" )
.collection( "Receipts" );
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_receipt );
mFirestoreList = findViewById(R.id.firestore_List);
setUpRecyclerView();
}
private void setUpRecyclerView() {
Query query = receiptRef = db.collection( "users" )
.document( "BtliRhtVFiQntlH2Hp1gvjG59b32" )
.collection( "Receipts" );
FirestoreRecyclerOptions<ReceiptsModel> options = new FirestoreRecyclerOptions.Builder<ReceiptsModel>()
.setQuery( query, ReceiptsModel.class )
.build();
//View holder class
adapter = new ReceiptAdapter( options );
mFirestoreList = findViewById( R.id.firestore_List );
mFirestoreList.setHasFixedSize( true );
mFirestoreList.setLayoutManager( new LinearLayoutManager( this ) );
mFirestoreList.setAdapter( adapter );
new ItemTouchHelper( new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
adapter.deleteItem( viewHolder.getAdapterPosition() );
}
} ).attachToRecyclerView( mFirestoreList );
adapter.setOnItemClickListener(new ReceiptAdapter.OnItemClickListener() {
@Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
ReceiptsModel receiptsModel = documentSnapshot.toObject(ReceiptsModel.class);
String id = documentSnapshot.getId();
String path = documentSnapshot.getReference().getPath();
Toast.makeText(ReceiptActivity.this,
"Position: " + position + " ID: " + id, Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
}
ReceiptAdapter.java
public class ReceiptAdapter extends FirestoreRecyclerAdapter<ReceiptsModel, ReceiptAdapter.ReceiptHolder> {
private OnItemClickListener listener;
public ReceiptAdapter(@NonNull FirestoreRecyclerOptions<ReceiptsModel> options) {
super( options );
}
@Override
protected void onBindViewHolder(@NonNull ReceiptHolder holder, int i, @NonNull ReceiptsModel model) {
holder.view_date.setText(model.getDate());
holder.view_store.setText(model.getStore());
holder.view_total.setText(String.valueOf(model.getTotal()));
}
@NonNull
@Override
public ReceiptHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_view, parent, false);
return new ReceiptHolder( v );
}
//delete receipt in the card view
public void deleteItem(int position) {
getSnapshots().getSnapshot( position ).getReference().delete();
}
class ReceiptHolder extends RecyclerView.ViewHolder {
TextView view_date;
TextView view_total;
TextView view_store;
public ReceiptHolder(@NonNull View itemView) {
super( itemView );
view_date = itemView.findViewById(R.id.view_date);
view_store = itemView.findViewById(R.id.view_store);
view_total = itemView.findViewById(R.id.view_total);
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(OnItemClickListener listener) {
this.listener = listener;
}
}
Step 1 - Make small changes here in your ReceiptActivity
adapter.setOnItemClickListener(new ReceiptAdapter.OnItemClickListener() {
@Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
ReceiptsModel model = documentSnapshot.toObject(ReceiptsModel.class);
Intent intent = new Intent(ReceiptActivity.this,DetailsActivity.class);
intent.putExtra("date",model.getDate());
intent.putExtra("email",model.getEmail());
intent.putExtra("store",model.getStore());
intent.putExtra("total",model.getTotal());
startActivity(intent);
}
});
Step 2
Create DetailsActivity with 4 textviews in activity_details.xml (id of textviews date,email,store,total)
public class DetailsActivity extends AppCompatActivity {
TextView date,email,store,total;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
date = findViewById(R.id.date);
email = findViewById(R.id.email);
store = findViewById(R.id.store);
total = findViewById(R.id.total);
date.setText(getIntent().getStringExtra("date")+"");
email.setText(getIntent().getStringExtra("email")+"");
store.setText(getIntent().getStringExtra("store")+"");
total.setText(getIntent().getStringExtra("total")+"");
}
}