how to implement a SetOnItemClickListener FirebaseRecyclerViewAdapter
There are probably many ways to do this, but this is the one I just quickly tried.
The ChatMessageViewHolder
class has a member field where it keeps the root view for each chat message:
public static class ChatHolder extends RecyclerView.ViewHolder {
View mView;
public ChatHolder(View itemView) {
super(itemView);
mView = itemView;
}
We can use that mView
member to get access to the clickable view. You should add a getter for mView
, but I'm trying to minimize the changes here.
So then in populateView
we can use the overload that has an additional position
parameter and wire things up with an OnClickListener
like this:
mRecycleViewAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(Chat.class, R.layout.message, ChatHolder.class, mChatRef) {
@Override
public void populateViewHolder(ChatHolder chatView, Chat chat, final int position) {
chatView.setName(chat.getName());
chatView.setText(chat.getText());
if (mAuthData != null && chat.getUid().equals(mAuthData.getUid())) {
chatView.setIsSender(true);
} else {
chatView.setIsSender(false);
}
chatView.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.w(TAG, "You clicked on "+position);
mRecycleViewAdapter.getRef(position).removeValue();
}
});
}
};
The answers to this question offer tons of inspiration: RecyclerView onClick. As long as you can determine the position that the user clicked on, you can use the mRecycleViewAdapter.getRef(position)
snippet to get the database reference of the item that was clicked.
Update
On second thought I might like this answer better https://stackoverflow.com/a/26196831/209103:
mMessages.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Log.w(TAG, "You clicked on "+position);
mRecycleViewAdapter.getRef(position).removeValue();
}
}));
It uses the RecyclerItemClickListener
helper class from the answer I linked. And as already said: as long as you have a click/touch listener that tells you the position of the item that was selected, the rest FirebaseUI aspect of it will be the same: call adapter.getRef(position)
to get a database reference for the selected item.
Filter in FirebaseRecyclerViewAdapter
Client-side filtering is not supported in the current version of FirebaseUI. It's on the roadmap, but not planned yet: https://github.com/firebase/FirebaseUI-Android/issues/15.
In the meantime, you can either use Firebase's built-in Query capabilities to limit the data. This has the advantage that only data matching the conditions will be downloaded from the server.
A great example of how advanced you can make this, is the "friend search" feature in the ShoppingList++ app of the Udacity course Firebase Essentials for Android. You can find the crucial class here in Github, but I highly recommend taking the entire course to see how this works.
Firebase RecyclerView ClickListener Not Being Called from .onStart()
Turns out this one line was screwing everything up and preventing the RecyclerView
views from being clickable:
android:clickable="true"
How to add Onclick listener to recycler view
You can handle this using two ways
1). Gesture touch
https://www.google.co.in/amp/sapandiwakar.in/recycler-view-item-click-handler/amp/
2).Using interface in adapter
https://antonioleiva.com/recyclerview-listener/
I suggest second way using interface
How to use interface for recycleritemclick
public class RecycleViewAdapter extends
RecyclerView.Adapter<RecycleViewHolder> {// Recyclerview will extend to
private List<FileName> fileNames;
private Context context;
//declare interface
private OnItemClicked onClick;
//make interface like this
public interface OnItemClicked {
void onItemClick(int position);
}
public RecycleViewAdapter(Context context,List<FileName> fileNames) {
this.context = context;
this.fileNames = fileNames;
}
Now assign click to interface
@Override
public void onBindViewHolder(RecycleViewHolder holder, final int position) {
//............//
holder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClick.onItemClick(position);
}
});
}
At the end of the adapter class up from finishing bracket make one method to assign itemclick to interface
public void setOnClick(OnItemClicked onClick){
this.onClick=onClick;
}
In MainActivity.java
Bind the item click with adapter
public class MainActivity extends Activity implements OnItemClicked {
private RecyclerView mRecyclerView;
private CityAdapter mAdapter;
private List<City> cities;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city);
mRecyclerView = (RecyclerView)findViewById(R.id.list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new CityAdapter(cities, R.layout.row_city, this);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnClick(MainActivity.this); // Bind the listener
}
@Override
public void onItemClick(int position) {
// The onClick implementation of the RecyclerView item click
//ur intent code here
}
}
For Kotlin code please refer How to handle recyclerview item click in kotlin?
More queries? comment.
Coupling FirebaseRecyclerViewAdapter to a Boolean/String Map.Entry
Your value is a Boolean
, not a Map
. So your comment is correct: you will need to specify Boolean
/Boolean.class
for the value type. To be able to then look up the key, you will need to upgrade to FirebaseUI-Android 0.2.2. In that release we added a populateViewHolder(VH, T, int)
overload that gets the position of the item as a parameter. With that, you can look up the key of the item.
Say this is your JSON structure:
{
"items": {
"pushid1": "Fri Jan 01 2016 16:40:54 GMT-0800 (PST)",
"pushid2": "Fri Jan 01 2016 16:41:07 GMT-0800 (PST)",
"pushid3": "Fri Jan 01 2016 16:41:25 GMT-0800 (PST)",
"pushid4": "Fri Jan 01 2016 16:41:37 GMT-0800 (PST)",
"pushid5": "Fri Jan 01 2016 16:42:04 GMT-0800 (PST)"
},
"index": {
"pushid1": true,
"pushid3": true,
"pushid5": true
}
}
So we store strings representing date/times and have an index to select a subset of these items.
We can now load the nodes from the index, then load the items that those nodes refer to and display them in the views with:
FirebaseRecyclerViewAdapter<Boolean, ItemViewHolder> adapter =
new FirebaseRecyclerViewAdapter<Boolean, ItemViewHolder>(
Boolean.class, android.R.layout.two_line_list_item, ItemViewHolder.class, ref.child("index")){
protected void populateViewHolder(final ItemViewHolder viewHolder, Boolean model, int position) {
String key = this.getRef(position).getKey();
ref.child("items").child(key).addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
String date = dataSnapshot.getValue(String.class);
((TextView)viewHolder.itemView.findViewById(android.R.id.text1)).setText(date);
}
public void onCancelled(FirebaseError firebaseError) { }
});
}
};
The output on screen:
For the full code, see Activity34559171
in this repo.
RecyclerView onClickListener with firebase database
Two Things that you have to change :
Make your object serialisable or parcelable.
public class Dentist implements Serializable{...}
OR
public class Dentist implements Parcelable{...}
Custom Object implement parcelable. This will help you implement parcelable in your object.
Pass object through intent
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View itemView) {
Intent intent = new Intent(itemView.getContext(), DetailsPage.class);
// get position
int pos = getAdapterPosition();
// check if item still exists
if(pos != RecyclerView.NO_POSITION){
Dentist clickedDataItem = listArray.get(pos);
Toast.makeText(itemView.getContext(), "You clicked " + clickedDataItem.getNom(), Toast.LENGTH_SHORT).show();
intent.putExtra("dentistObject", clickedDataItem);
itemView.getContext().startActivity(intent);
}
}
});
Related Topics
How to Make the Text Direction from Right to Left
How to Dynamically Create Menu Items
What's Better? Notifydatasetchanged or Notifyitemchanged in Loop
Hw Accelerated Activity - How to Get Opengl Texture Size Limit
Cannot Resolve Method Setlatesteventinfo
List of Files in Assets Folder and Its Subfolders
Android: How to Share Image with Text on Facebook via Intent
Determining If an Android Device Is Rooted Programmatically
Detect Power Button Long Press
How to Store SQLite Database Directly on Sdcard
Horizontal Scrolling Grid View
How to Run Media Scanner in Android
Android Exception: Did You Forget to Call 'Public Void Setup (Localactivitymanager Activitygroup)'
Vertical Line Using Xml Drawable