Я использовал переполнение стека, чтобы передать позицию адаптера со своей страницы событий с помощью Firebase Recycler View в мой подробный вид, чтобы я мог показать только это событие, однако написанный мною код, похоже, не работает.
Я уверен, что это правильно, но я упустил что-то очевидное.
Вот мой MainActivity
открытый класс EventsDisplayActivity расширяет AppCompatActivity {
private RecyclerView mRecylerView;
private DatabaseReference mDatabase;
private EditText mSearch;
private FirebaseRecyclerAdapter<Events, EventsDisplayActivity.EventsViewHolder> mEventsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events_display);
setTitle("Events");
//"Events" here will reflect what you have called your database in Firebase.
mDatabase = FirebaseDatabase.getInstance().getReference().child("Events");
mDatabase.keepSynced(true);
mRecylerView = findViewById(R.id.result_list);
mSearch = findViewById(R.id.editTextSearch);
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference().child("Events");
Query query = dbRef.orderByKey();
//Query search = dbRef.orderByChild("name").startAt(searchText).endAt(searchText + "\uf8ff");
mRecylerView.hasFixedSize();
mRecylerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<Events>().setQuery(query, Events.class).build();
mEventsAdapter = new FirebaseRecyclerAdapter<Events, EventsDisplayActivity.EventsViewHolder>(options) {
@Override
protected void onBindViewHolder(EventsViewHolder holder, final int position, final Events model) {
holder.setName(model.getName());
holder.setDescription(model.getDescription());
holder.setImage(getBaseContext(), model.getImage());
}
@Override
public EventsDisplayActivity.EventsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.event_list, parent, false);
return new EventsDisplayActivity.EventsViewHolder(view);
}
};
mRecylerView.setAdapter(mEventsAdapter);
}
@Override
public void onStart() {
super.onStart();
mEventsAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
mEventsAdapter.stopListening();
}
public static class EventsViewHolder extends RecyclerView.ViewHolder{
View mView;
public EventsViewHolder(View itemView){
super(itemView);
mView = itemView;
mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, EventDetailActivity.class);
intent.putExtra(EventDetailActivity.EXTRA_POSITION, getAdapterPosition());
context.startActivity(intent);
}
});
}
public void setName(String title){
TextView post_title = mView.findViewById(R.id.event_title);
post_title.setText(title);
}
public void setDescription(String desc){
TextView post_desc = mView.findViewById(R.id.event_desc);
post_desc.setText(desc);
}
public void setImage(Context context, String image){
ImageView post_image = mView.findViewById(R.id.event_image);
Picasso.with(context).load(image).into(post_image);
}
}
}
А вот подробное действие, которое по какой-то причине не получает намерения.
открытый класс EventDetailActivity расширяет AppCompatActivity {
public static final String EXTRA_POSITION = "position";
private DatabaseReference mDatabase;
private DatabaseReference mEventReference;
private ValueEventListener mEventListener;
private String mEventKey;
private TextView mDetailName;
private TextView mDetailDesc;
private ImageView mDetailImage;
private static final String TAG = "EventDetailActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_detail);
//Getting the selected event from intent
mEventKey = getIntent().getStringExtra(EXTRA_POSITION);
if(mEventKey == null) {
throw new IllegalArgumentException("Must pass EXTRA_POSITION");
}
//Init the DB
mDatabase = FirebaseDatabase.getInstance().getReference().child("Events").child(mEventKey);
mDatabase.keepSynced(true);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Set Collapsing Toolbar layout to the screen
CollapsingToolbarLayout collapsingToolbar =
findViewById(R.id.collapsing_toolbar);
// Set title of Detail page
// collapsingToolbar.setTitle(getString(R.string.item_title));
//Init Views
mDetailName = findViewById(R.id.event_title_detail);
mDetailDesc = findViewById(R.id.event_desc_detail);
mDetailImage = findViewById(R.id.event_image_detail);
}
public void onStart() {
super.onStart();
// Event Listener to the view
// [Start event_value_event_listener]
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Event Object and use the values to update said UI
Events events = dataSnapshot.getValue(Events.class);
// [START_EXCLUDE]
mDetailName.setText(events.name);
mDetailDesc.setText(events.description);
// [END_EXCLUDE]]
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// [START_EXCLUDE]
Toast.makeText(EventDetailActivity.this, "Failed to load events.",
Toast.LENGTH_SHORT).show();
// [END_EXCLUDE]
}
};
mEventReference.addValueEventListener(eventListener);
// [END post_value_event_listener]
// Keep copy of post listener so we can remove it when app stops
mEventListener = eventListener;
}
@Override
public void onStop() {
super.onStop();
// Remove post value event listener
if (mEventListener != null) {
mEventReference.removeEventListener(mEventListener);
}
}
}
Цените это!