I have a recyclerView which items of recyclerView has a textView sometimes with links in it. I use richLinkPreview for previewing my link inside each item has a link. But when I scroll the recyclerView I face a problem that is every time I view an item with link, it goes to bring link data and show it and sometimes it shows wrong link preview(shows link preview of item A in item B). I think my problem is calling richLinkPreview inside onBindViewHolder method but I have not any option in my knowledge. Could you help me with that?
Try this:
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private final SparseArray<MetaData> mFetchedMetadata = new SparseArray<>();
...
#Override
public void onBindViewHolder(final #NonNull Adapter.ViewHolder holder,
final int position) {
...
final MetaData metadata = mFetchedMetadata.get(position);
if (metadata != null) {
holder.richLinkView.setLinkFromMeta(metadata);
} else {
holder.richLinkView.setLink("url", new ViewListener() {
#Override
public void onSuccess(boolean status) {
mFetchedMetadata.put(position, holder.richLinkView.getMetaData());
}
#Override
public void onError(Exception e) {
}
});
}
...
}
...
}
Related
I am getting duplicate items from an API in my adapter but I want to remove those duplicates from my side means from my adapter and print it once any idea how? Thanks in advance.
got: duplicate rows in my cardview but through an API.
want: just want to print it once and remove those duplicates.
MyAdapter:
#Override
public void onBindViewHolder(#NonNull catView holder, int position) {
holder.id.setText(oilResponses.get(position).getId());
}
#Override
public int getItemCount() {
if (oilResponses.size() != 0) {
return oilResponses.size();
}
return 0;
}
MainActivity:
private void oilList() {
Api.getClient().do_oil(CAR_ID)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Observer<List<OilResponse>>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(#NotNull List<OilResponse> responses) {
oilAdapter = new OilAdapter(responses);
oilRecycler.setAdapter(oilAdapter);
}
#Override
public void onError(#NotNull Throwable e) {
}
#Override
public void onComplete() {
}
});
}
you can try to save them in a Set instead of a List, it is basically the same thing but in a set each object can be saved one time, so you can't have duplicates even if you'd like to.
I have in my MainActivity 3 RecyclerViews .
One of them in a bottom sheet and it is the main one (By Default the Bottom sheet is Open To Display this one ), in its adapter's onbind method I made an onClickListener so that I want when the user clicks on an item in it,
I want to go back to the main activity class to set To Start a method which it's rolled is to close the Bottom Sheet and set the data for the next recycling view (which will appear when the Bottom Sheet is closed)
..... The issue here is how to start this method from the onBind method's Listener and give it a parameter from this viewHolder as its name and some of its attributes
if there is something not clear please let me know
#Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, final int position) {
viewHolder.categoryImage.setImageResource(mRowOfCategories.get(position).getCategoryImage());
viewHolder.categoryName.setText(mRowOfCategories.get(position).getCategoryName());
viewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
Easiest:
You declare the method you want to call in your Activity. It has to be public:
public void method(){}
Then, in the Constructor of the Adapter, you pass a reference to the Activity as a parameter:
public MyAdapter(Activity activity){}
And, in the onBindViewHolder:
MyActivity mActivity=(MyActivity)activity;
mActivity.method();
You can do that easily.
Define custom interface
public Interface CustomEventListener
{
public void MyEventListener(String message); //you can change parameters
}
In your adapter class
public Adapter ......... {
private CustomEventListener listener;
public void setListener(CustomEventListener listener)
{
this.listener = listener;
}
//Your onBind
Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, final int position) {
viewHolder.categoryImage.setImageResource(mRowOfCategories.get(position).getCategoryImage());
viewHolder.categoryName.setText(mRowOfCategories.get(position).getCategoryName());
viewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null)
listener.MyEventListener("Message");
}
});
}
}
In your Activity when create Adapter add this code
public void InitAdapter()
{
yourAdapter = new Adapter(); // bloa bla bla
yourAdapter.setListener(new CustomEventListener() {
public void MyEventListener(String message)
{
// then do what you want
}
}
}
You need to use Listener for handling the click in your Adapter something like this:
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
then call the listener with:
#Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, final int position) {
...
viewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// call the listener.
mListener.onItemClick(v, viewHolder.getAdapterPosition());
}
});
}
then when you're using the adapter, set the listener with something like this:
adapter.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(View view, int position) {
// do something with the view and position.
}
});
You want your recycleviews to be aware of each other. You'll need a class, maybe MainActivity, that will keep the selected value, also clear it upon request, in between recycleviews. Then you'll need that value to propagate the next recycleview. If you want to be efficient then you'll use one recycleview and swap the data in between selections, and animate it so it looks like a new recycleview is created.
I have an Android app which uses firestore as its database. I have followed this series of blog posts to set up my firestore database in my app : https://firebase.googleblog.com/2017/12/using-android-architecture-components.html and then followed this stackoverflow entry to change my code to work for firestore: Android Architecture Components with Firebase specifically Firestore.
After this I was successful to display the result of my query in a recycler view, however when I added the swap to update (I do soft delete by setting a isActive flag to false) action in my app, LiveData was inconsistent in refreshing the RecyclerView. Here is my code snippets:
MainActivity.java
TaskViewModel viewModel =
ViewModelProviders.of(this).get(TaskViewModel.class);
LiveData<LinkedList<TaskProperties>> liveData = viewModel.getTaskPropertiesLiveData();
final MainActivity mainActivityReference = this;
liveData.observe(this, new Observer<LinkedList<TaskProperties>>() {
#Override
public void onChanged(#Nullable LinkedList<TaskProperties> taskProperties) {
if (taskProperties != null) {
// Get a handle to the RecyclerView.
mRecyclerView = findViewById(R.id.recyclerview);
// Create an adapter and supply the data to be displayed.
mAdapter = new TaskListAdapter(mainActivityReference, taskProperties);
// Connect the adapter with the RecyclerView.
ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(mAdapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(mRecyclerView);
mRecyclerView.setAdapter(mAdapter);
// Give the RecyclerView a default layout manager.
mRecyclerView.setLayoutManager(new LinearLayoutManager(mainActivityReference));
}
}
});
View Model:
public class TaskViewModel extends ViewModel {
private LinkedList<TaskProperties> taskProperties;
private static final Query PROJECT_REF = FirebaseFirestore.getInstance().collection("project").whereEqualTo("active", true);
private final FirebaseQueryLiveData liveData = new FirebaseQueryLiveData(PROJECT_REF);
public TaskViewModel() {
taskPropertiesLiveData.addSource(liveData, new Observer<QuerySnapshot>() {
#Override
public void onChanged(#Nullable final QuerySnapshot querySnapshot) {
if (querySnapshot != null) {
new Thread(new Runnable() {
#Override
public void run() {
taskProperties = new LinkedList<TaskProperties>();
for (DocumentSnapshot document : querySnapshot.getDocuments()) {
taskProperties.addLast(document.toObject(TaskProperties.class));
}
taskPropertiesLiveData.postValue(taskProperties);
}
}).start();
} else {
taskPropertiesLiveData.setValue(null);
}
}
});
}
#NonNull
public LiveData<LinkedList<TaskProperties>> getTaskPropertiesLiveData() {
return taskPropertiesLiveData;
}
}
Code in the callback class to remove :
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
}
Constructor in Adapter:-
public TaskListAdapter(Context context,LinkedList<TaskProperties> taskList) {
mInflater = LayoutInflater.from(context);
this.taskList = taskList;
}
Code in Adapter to remove:-
public void onItemDismiss(int position) {
TaskDao taskDao = new TaskDao();
taskDao.softDeleteTaskInDB(taskList.get(position));
}
Code in DAO class to update( soft delete) :-
public void softDeleteTaskInDB(TaskProperties taskProperties){
taskProperties.setActive(false);
database.collection("project")
.document(taskProperties.getTask())
.set(taskProperties).
addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d(DEBUG_TAG, "DocumentSnapshot successfully written!");
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w(DEBUG_TAG, "Error writing document", e);
}
});
Log.i(DEBUG_TAG,taskProperties.getTask());
}
I have observed that LiveData was able to refresh the view when I was deleting one component from the end of the list, however when I deleted from the middle of the list the view sometimes does not refresh properly. From the logs I found that the position that is being passed into the adapter class is working fine, however the tasklist array does not have the most updated value.
For example if the task list contains :-
Cat
Dog
Mouse
Rabbit
Tiger
and if delete Mouse and then Rabbit in quick succession, the onItemDismiss in adapter class receives position 3 in both cases, but the taskList variable in the Adapter class still contains Mouse at position 3. This means the LiveData might not have refreshed the RecyclerView.
Can someone please tell me where am I going wrong?
Thanks,
Sangho
I'm using firebase for chat app and I'm implementing to fetch messages from firebase database and update recycler view.
When I click the "send" button, it acts like below.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final ChatModel.Comment comment = new ChatModel.Comment();
comment.uid = uid;
comment.message = editText.getText().toString();
FirebaseDatabase.getInstance().getReference().child("chatrooms")
.child(chatroomUid).push().setValue(comment).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
FirebaseDatabase.getInstance().getReference().child("chatrooms")
.child(chatroomUid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
comments.clear();
for(DataSnapshot item : dataSnapshot.getChildren()) {
if(item.getKey().compareTo("users")!=0) {
comments.add(item.getValue(ChatModel.Comment.class));
}
}
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
});
}
});
Process is like this.
Store message data into firebase database using "Comment" object form.
If stage 1 is succeeded, fetch that message and call notifyDataSetChanged()
But it is not working, I have global adpater object and above method is called by this variable.
Adapter code is like below.
private class messageRecyclerveiwAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_message, viewGroup, false);
return new MessageViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder viewHolder, int i) {
((MessageViewHolder)viewHolder).textView.setText(comments.get(i).message);
}
#Override
public int getItemCount() {
return comments.size();
}
}
How can I fix it?
You need to create an object of your Adapter in the same class as you have the setOnClickListener:
Something like this:
final MessageRecyclerViewAdapter adapter = new MessageRecyclerViewAdapter(getActivity(), messageArrayList);
Then you need to pass the adapter to the method you're using to get the messages. Right now, you're calling notifyDataSetChanged() in the adapter which is wrong. First you need a specific method to get those messages from Firebase. For example:
public void getLiveChatMessages(final ArrayList<ChatMessageClass> messageArrayList, final MessageRecyclerViewAdapter adapter) {
I'm guessing you have a class that returns different part of the messages.. That's what I've named "ChatMessageClass" here, but you may call it something else. Within this method you get the messages from Firebase, then afterward you call as the last thing in the method:
adapter.notifyDataSetChanged();}
within the getLiveChatMessages method. Here you also pass in the adapter so that you can use it. This is the way to call the notifyDataSetChanged() within the same method as where you get the messages.
Good luck!
Simple thing I would like to do (see in the picture)
Display a view with info coming from 2 different places in Firebase so that it behaves in a professional way scrolling UP and DOWN
I have a list of movies and on each of them I would like the user to specify a rating and see it
In DB I created 2 structures to have the list of movies on one side and the ratings per user on the other
Problem using FirebaseRecyclerAdapter
My problem is that scrolling fast up and down the list, the visualization of the information coming from the second reference (the rating) is loaded on a different time (asynchronous call) and this is not acceptable to see this (little) delay building the view. Is this a limitation of FirebaseRecyclerView?
Because viewHolders are reused in the recycleView I reset and reload each time in populateView() the rating values and this doesn't help. Once retrieved I'm oblidged to get them again if the user scroll the view (see the setOnlistener in populateView()
Setting a listener in populateView cause also to have as many listener as the number of times populateView() is executed (if you scroll UP and DOWN it's many times).
Solutions / Workaround ?
Is there a correct way to do it preventing the problem? Or is it a limitation?
What about performance with my implementation where the listener is inside populateView() and there are MANY listener created?
Below some things I'm thinking on:
Prevent viewHolders to be recycled and just load once?
Override some other methods of RecyclerView? I tried with parseSnapshot() but it's the same problem...
Change the DB structure to have all the info in one list (I don't think it's the good one because it means adding rating information of each user to movie list)
Add a loading spinner on the rating part so that the rating is displayed only when the asyncrhonous call to firebase is completed (don't like it) without the today effect of: "changing star color in front of the user".
My Implementation
From FirebaseRecyclerAdapter
#Override
protected void populateViewHolder(final MovieViewHolder viewHolder, final Movie movie, final int position) {
String movieId = this.getRef(position).getKey();
// Oblidged to show no rating at the beginning because otherwise
// if a viewHolder is reused it has the values from another movie
viewHolder.showNoRating();
//---------------------------------------------
// Set values in the viewHolder from the model
//---------------------------------------------
viewHolder.movieTitle.setText(movie.getTitle());
viewHolder.movieDescription.setText(movie.getDescription());
//-----------------------------------------------------
// Ratings info are in another DB location... get them
// but call is asynchronous so PROBLEM when SCROLLING!
//-----------------------------------------------------
DatabaseReference ratingMovieRef = mDbRef.child(Constants.FIREBASE_LOCATION_RATINGS).child(currentUserId).child(movieId);
ratingQuoteRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
RatingMovie ratingMovie = dataSnapshot.getValue(RatingMovie.class);
Rating rating = Rating.NO_RATING;
if (ratingMovie != null) {
rating = Rating.valueOf(ratingMovie.getRating());
}
// Set the rating in the viewholder (through anhelper method)
viewHolder.showActiveRating(rating);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
from MovieViewHolder
public class QuoteViewHolder extends RecyclerView.ViewHolder {
public CardView cardView;
public TextView movieTitle;
public TextView movieDescription;
public ImageView ratingOneStar;
public ImageView ratingTwoStar;
public ImageView ratingThreeStar;
public QuoteViewHolder(View itemView) {
super(itemView);
movieTitle = (TextView)itemView.findViewById(R.id.movie_title);
movieDescription = (TextView)itemView.findViewById(R.id.movie_descr);
// rating
ratingOneStar = (ImageView)itemView.findViewById(R.id.rating_one);
ratingTwoStar = (ImageView)itemView.findViewById(R.id.rating_two);
ratingThreeStar = (ImageView)itemView.findViewById(R.id.rating_three);
}
/**
* Helper to show the color on stars depending on rating value
*/
public void showActiveRating(Rating rating){
if (rating.equals(Rating.ONE)) {
// just set the good color on ratingOneStar and the others
...
}
else if (rating.equals(Rating.TWO)) {
// just set the good color
...
} else if (rating.equals(Rating.THREE)) {
// just set the good color
...
}
/**
* Initialize the rating icons to unselected.
* Important because the view holder can be reused and if not initalised values from other moviecan be seen
*/
public void initialiseNoRating(){
ratingOneStar.setColorFilter(ContextCompat.getColor(itemView.getContext(), R.color.light_grey));
ratingTwoStar.setColorFilter(....
ratingThreeStar.SetColorFilter(...
}
You can sort of cache the ratings using a ChildEventListener. Basically just create a separat one just for the Ratings node, and have it store the ratings in a Map. Then using the RecyclerAdapter you will retrieve from the Map if the rating is available, if it is not, have the rating listener update the recyclerview as soon as is has downloaded the rating. This is one strategy you could go about, doing it, you will have to manually copy/paste some classes from the FirebaseUI library and set some fields public for this to work.
Usage would be something like this
private MovieRatingConnection ratingConnection;
// inside onCreate
ratingConnection = new MovieRatingConnection(userId, new MovieRatingConnection.RatingChangeListener() {
#Override
public void onRatingChanged(DataSnapshot dataSnapshot) {
if (recyclerAdapter != null) {
if (dataSnapshot != null) {
int index = recyclerAdapter.snapshots.getIndexForKey(dataSnapshot.getKey());
recyclerAdapter.notifyItemChanged(index);
}
}
}
});
Query movieQuery = FirebaseDatabase.getInstance().getReference().child("Movies");
recyclerAdapter = new FirebaseRecyclerAdapter(movieQuery...) {
#Override
public void populateViewHolder(RecyclerView.ViewHolder viewHolder, Object model, int position) {
//...
final String key = getRef(position).getKey();
viewHolder.showActiveRating(ratingConnection.getRating(key));
}
};
and MovieRatingConnection would be a class like this
public class MovieRatingConnection {
private MovieRatingListener listener;
public MovieRatingConnection(String userId, RatingChangeListener changeListener) {
Query query = FirebaseDatabase.getInstance().getReference().child("MovieRatings").child(userId);
listener = new MovieRatingListener(query, changeListener);
}
public Rating getRating(String key) {
return listener.getRating(key);
}
public void cleanup() {
if (listener != null) {
listener.unregister();
}
}
public static class MovieRatingListener implements ChildEventListener {
public interface RatingChangeListener {
public void onRatingChanged(DataSnapshot snapshot);
}
private Query query;
private HashMap<String, Rating> ratingMap = new HashMap<>();
private RatingChangeListener changeListener;
public MovieRatingListener(Query query, RatingChangeListener changeListener) {
this.query = query;
this.changeListener = changeListener;
query.addChildEventListener(this);
}
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot != null) {
ratingMap.put(dataSnapshot.getKey(), dataSnapshot.getValue(Rating.class));
changeListener.onRatingChanged(dataSnapshot);
}
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot != null) {
ratingMap.put(dataSnapshot.getKey(), dataSnapshot.getValue(Rating.class));
changeListener.onRatingChanged(dataSnapshot);
}
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
ratingMap.remove(dataSnapshot.getKey());
changeListener.onRatingChanged(null);
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
public Rating getRating(String key) {
if (ratingMap.get(key) != null) {
return ratingMap.get(key);
} else {
return new Rating(); // default value/null object
}
}
public void unregister() {
query.removeEventListener(this);
}
}
}