'Sending detailed Firebase data to another activity in android
I am new to mobile programming and I want to make a simple android app using some data stored in Firebase. I managed to create a RecyclerView with all the children of my Firebase collection and I want to press a button to navigate to a more detailed view of a specific child (for example to see a bigger image, a complete description etc). Here I am encountering a few errors. I have created an adapter, like this:
public class MainAdapter extends FirebaseRecyclerAdapter<MainModel, MainAdapter.viewHolder> {
Context context;
public MainAdapter(@NonNull FirebaseRecyclerOptions<MainModel> options, Context context) {
super(options);
this.context = context;
}
@Override
protected void onBindViewHolder(@NonNull viewHolder holder, @SuppressLint("RecyclerView") final int position, @NonNull MainModel model) {
//viewHolder properties initialization
holder.btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("dresses");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String ma = snapshot.getValue().toString();
String name = snapshot.child("name").getValue().toString();
String brand = snapshot.child("brand").getValue().toString();
String description = snapshot.child("description").getValue().toString();
String image = snapshot.child("image").getValue().toString();
String url = snapshot.child("url").getValue().toString();
String price = snapshot.child("price").getValue().toString();
Intent intent = new Intent();
intent.putExtra("dress" , String.valueOf(ma));
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
Intent intent = new Intent(context.getApplicationContext(), DetailActivity.class);
context.startActivity(intent);
}
});
}
}
So when I am pressing the button with the id btnView I want to navigate to my Detailed Activity, which is this:
public class DetailActivity extends AppCompatActivity {
TextView name, brand, url, price, description;
ImageButton fav;
ImageView image;
ConstraintLayout view;
MainAdapter mainAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
name = findViewById(R.id.nameDress);
brand = findViewById(R.id.brandDress);
url = findViewById(R.id.urlDress);
url.setMovementMethod(LinkMovementMethod.getInstance());
price = findViewById(R.id.priceDress);
description = findViewById(R.id.descriptionDress);
fav = findViewById(R.id.imageButton);
image = findViewById(R.id.imageDress);
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("dresses");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
name = (TextView) snapshot.child("name").getValue();
brand = (TextView) snapshot.child("brand").getValue();
description = (TextView) snapshot.child("description").getValue();
image = (ImageView) snapshot.child("image").getValue();
url = (TextView) snapshot.child("url").getValue();
price = (TextView) snapshot.child("price").getValue();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
@Override
protected void onStart() {
super.onStart();
mainAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
mainAdapter.stopListening();
}
}
So basically I want to find a way to send the data from my recycler view item in the main activity to my detailed activity when pressing on the view button. The problem is that the context seems to be null in my adapter, because I'm getting this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
I tried to find a way to inject it in this component somehow, but I realised it's not exactly how it would normally work in Java. How should I solve this?
Solution 1:[1]
You haven't initialized the context in the adapter code. The context could be initialized by passing it in the adapter constructor e.g.
public MainAdapter(@NonNull FirebaseRecyclerOptions<MainModel> options, Context context) { this.context = context; super(options); }To pass the "MainModel" class to DetailsActivity, you can implement Parcelable or Serializable (I would go with Parcelable since it's faster) and send the "MainModel" class via intent e.g.
intent.putExtra("your_intent_key", mainModel);
And to get the MainModel class in DetailsActivity, in onCreate() you can:
mainModel = getIntent().getParcelableExtra("your_intent_key");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Frank van Puffelen |
