FirestoreRecyclerAdapter not updated after new firestore query - java

I have a FloatingSearchView in my app to perform some query on my Firestore database. When I check the size of each query, the result is as expected but my view is not updating with the result. I don't understand if this is the queries or if this is how I handle the different adapter.
I have one FirestoreRecyclerAdapter for each query. I don't understand what's wrong. Thank you for your help!
floatingSearchView.setOnSearchListener(new
FloatingSearchView.OnSearchListener() {
#Override
public void onSuggestionClicked(SearchSuggestion
searchSuggestion) {
mLastQuery = searchSuggestion.getBody();
com.google.firebase.firestore.Query qSuggestion =
db
.collection("article")
.whereEqualTo("category", category_key)
.whereEqualTo("type", mLastQuery);
qSuggestion
.get()
.addOnSuccessListener(new
OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot
documentSnapshots) {
int size = documentSnapshots
.getDocuments()
.size();
Toast.makeText(Blog.this, "size " + size,
Toast.LENGTH_LONG).show();
}
});
FirestoreRecyclerOptions<Blog_model> opt = new FirestoreRecyclerOptions.Builder<Blog_model>()
.setQuery(qSuggestion, Blog_model.class)
.build();
Log.d("option", opt.getSnapshots().toString());
suggestionAdapter = new FirestoreRecyclerAdapter<Blog_model, Blog.BlogViewHolder>(opt) {
#Override
public void onBindViewHolder(#NonNull Blog.BlogViewHolder holder, int position, #NonNull final Blog_model model) {
holder.setTitle(model.getTitle());
holder.setDesc(model.getDesc());
holder.setImage(getApplicationContext(), model.getPicture());
final String post_key = getSnapshots().getSnapshot(position).getId();
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Ordinary Intent for launching a new activity
final Intent intent = new Intent(Blog.this, BlogDetails.class);
intent.putExtra("article_id", post_key);
intent.putExtra("category_key", category_key);
intent.putExtra("image", model.getPicture());
intent.putExtra("title", model.getTitle());
startActivity(intent);
}
});
}
#Override
public Blog.BlogViewHolder onCreateViewHolder(ViewGroup group, int i) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(group.getContext())
.inflate(R.layout.blog_row, group, false);
return new Blog.BlogViewHolder(view);
}
};
floatingSearchView.clearSearchFocus();
mBlogList.setAdapter(suggestionAdapter);
}
#Override
public void onSearchAction(String currentQuery) {
mLastQuery = currentQuery;
// query to firebase
com.google.firebase.firestore.Query qSuggestion =
db
.collection("article")
.whereEqualTo("keyword."+mLastQuery, true);
FirestoreRecyclerOptions<Blog_model> options1 = new FirestoreRecyclerOptions.Builder<Blog_model>()
.setQuery(qSuggestion, Blog_model.class)
.build();
Log.d("option", options1.getSnapshots().toString());
searchAdapter = new FirestoreRecyclerAdapter<Blog_model, Blog.BlogViewHolder>(options1) {
#Override
public void onBindViewHolder(#NonNull Blog.BlogViewHolder holder, int position, #NonNull final Blog_model model) {
holder.setTitle(model.getTitle());
holder.setDesc(model.getDesc());
holder.setImage(getApplicationContext(), model.getPicture());
final String post_key = getSnapshots().getSnapshot(position).getId();
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(Blog.this, "title", Toast.LENGTH_LONG).show();
// Ordinary Intent for launching a new activity
final Intent intent = new Intent(Blog.this, BlogDetails.class);
intent.putExtra("article_id", post_key);
intent.putExtra("category_key", category_key);
intent.putExtra("image", model.getPicture());
intent.putExtra("title", model.getTitle());
startActivity(intent);
}
});
}
#Override
public Blog.BlogViewHolder onCreateViewHolder(ViewGroup group, int i) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(group.getContext())
.inflate(R.layout.blog_row, group, false);
return new Blog.BlogViewHolder(view);
}
};
mBlogList.setAdapter(searchAdapter);
}
});
#Override
public void onStart() {
super.onStart();
adapter.startListening();
if(suggestionAdapter != null){
suggestionAdapter.startListening();
}
if(searchAdapter != null){
searchAdapter.startListening();
}
}
#Override
public void onStop() {
super.onStop();
adapter.stopListening();
if(suggestionAdapter != null){
suggestionAdapter.stopListening();
}
if(searchAdapter != null){
searchAdapter.stopListening();
}
}

You are using two FirestoreRecyclerAdapter objects, which is correct, but the problem in your code is that you are not listening to the second adapter for changes in the right place. To solve this, add inside onSearchAction method:
searchAdapter.startListening();
Right after you create the adapter object. This means that for every character that you type in your FloatingSearchView, you create a new adapter and you populate it with the results that are coming from the database. If you are starting listening in the onStart method, it doesn't help you at all.

Related

How to fix static/non-static conflict between .this and .setOnItemClickListener?

I want to be able to click on an info button in my recyclerview "planner" so I can see my (firestore) participants inside that particular event/day.
But I'm getting an error I cannot fix.
The biggest trouble I'm having is the fact im working within fragments. So copying Youtube doenst always work. + I'm new to coding and java.
This want to make it non static
but when I fix that:
This wants to make it static again
Inside LesAdapter
public LesViewHolder(#NonNull View itemView){
super(itemView);
Lijst_Soort = itemView.findViewById(R.id.idLes);
Lijst_Waar = itemView.findViewById(R.id.idWaar);
Lijst_Wanneer = itemView.findViewById(R.id.idWanneer);
btnInfo = itemView.findViewById(R.id.btnInfo);
btnInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
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;
}
Inside HomeFragment could using LesAdapter before .setOnItemClickListener be wrong?
LesAdapter.setOnItemClickListener(new LesAdapter.OnItemClickListener() {
#Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
String wanneer = documentSnapshot.getString("Wanneer");
Intent intent = new Intent(getActivity(), Deelnamelijst.class);
intent.putExtra("Welke les", wanneer);
startActivity(intent);
}
});
This is what i was trying:
https://www.youtube.com/watch?v=3WR4QAiVuCw
https://www.youtube.com/watch?v=ZXoGG2XTjzU
The error is that trying to call LesAdapter.setOnItemClickListener assumes that the setOnItemClickListener method is a static method and hence you can call it without creating an instance.
For your example, setOnItemClickListener is not a static method and hence to call it, you need an instance of LesAdapter. So use whatever instance you creating like
LesAdapter lesAdapter = new LesAdapter();
lesAdapter.setOnItemClickListener(new LesAdapter.OnItemClickListener() {
#Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
String wanneer = documentSnapshot.getString("Wanneer");
Intent intent = new Intent(getActivity(), Deelnamelijst.class);
intent.putExtra("Welke les", wanneer);
startActivity(intent);
}
});

Delete message in chat activity nothing happen

I am trying to make android app for chatting when I send message done see it done but when I am trying to delete It nothing happens ... this is my adapter chat ... dialog message appear but when I am clicking on delete nothing happen ... when i am click on no dismiss dialog happen it is fine ... but nothing with delete ... any one can help me please ?
public class AdapterChat extends RecyclerView.Adapter<AdapterChat.MyHolder> {
private static final int MSG_TYPE_LEFT = 0;
private static final int MSG_TYPE_RIGHT = 1;
Context context;
List<Modelchat> chatList;
String imageUrl;
FirebaseUser fUser;
public AdapterChat(Context context, List<Modelchat> chatList, String imageUrl) {
this.context = context;
this.chatList = chatList;
this.imageUrl = imageUrl;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
if (i == MSG_TYPE_RIGHT) {
View view = LayoutInflater.from(context).inflate(R.layout.row_chat_right, viewGroup, false);
return new MyHolder(view);
} else {
View view = LayoutInflater.from(context).inflate(R.layout.row_chat_left, viewGroup, false);
return new MyHolder(view);
}
}
#Override
public void onBindViewHolder(#NonNull MyHolder myHolder, final int i) {
String message = chatList.get(i).getMessage();
String timeStamp = chatList.get(i).getTimestamp();
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(Long.parseLong(timeStamp));
String dataTime = DateFormat.format("dd/MM/yyyy hh:mm aa", cal).toString();
myHolder.messageTv.setText(message);
myHolder.timeTv.setText(dataTime);
try {
Picasso.get().load(imageUrl).into(myHolder.profileTv);
} catch (Exception e) {
}
//show delete dialog message
myHolder.messageLAyout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Delete");
builder.setMessage("Are you sure to delete this message?");
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
deleteMessage(i);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
});
if (i == chatList.size() - 1) {
if (chatList.get(i).isSeen()) {
myHolder.isSeenTv.setText("Seen");
} else {
myHolder.isSeenTv.setText("Delivered");
}
} else {
myHolder.isSeenTv.setVisibility(View.GONE);
}
}
private void deleteMessage(int position) {
String myUID = FirebaseAuth.getInstance().getCurrentUser().getUid();
String msgTimeStamp = chatList.get(position).getTimestamp();
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference("Chats");
Query query = dbRef.orderByChild("timestamp").equalTo(msgTimeStamp);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
if (ds.child("sender").getValue().equals(myUID)) {
// ds.getRef().removeValue();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("message", "This message was deleted...");
ds.getRef().updateChildren(hashMap);
chatList.remove(i);
notifyItemRemoved(i);
notifyItemRangeChanged(i, chatList.size());
Toast.makeText(context, "message deleted...", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "You can delete only your messages...", Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public int getItemCount() {
return chatList.size();
}
#Override
public int getItemViewType(int position) {
fUser = FirebaseAuth.getInstance().getCurrentUser();
if (chatList.get(position).getSender().equals(fUser.getUid())) {
return MSG_TYPE_RIGHT;
} else {
return MSG_TYPE_LEFT;
}
}
class MyHolder extends RecyclerView.ViewHolder {
ImageView profileTv;
TextView messageTv, timeTv, isSeenTv;
LinearLayout messageLAyout;
public MyHolder(#NonNull View itemView) {
super(itemView);
profileTv = itemView.findViewById(R.id.profileTv);
messageTv = itemView.findViewById(R.id.messageTv);
timeTv = itemView.findViewById(R.id.timeTv);
isSeenTv = itemView.findViewById(R.id.isSeenTv);
messageLAyout = itemView.findViewById(R.id.messageLayout);
}
}
}
Emulator
I assume by delete you mean changing the text of the message to "This message was deleted...", if that is the case then the issue is that you update the database and don't update the object in the list:
Do this in deleteMessage(int position) function:
....
....
if (ds.child("sender").getValue().equals(myUID)) {
//ds.getRef().removeValue();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("message", "This message was deleted...");
ds.getRef().updateChildren(hashMap);
Toast.makeText(context, "message deleted...", Toast.LENGTH_SHORT).show();
//here add these lines to update the list value.........
chatList.get(position).setMessage("This message was deleted...");
notifyDataSetChanged();
......
......
Just delete message success remove that position from list and notifyItemRemoved() try following code
if (ds.child("sender").getValue().equals(myUID)) {
// ds.getRef().removeValue();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("message", "This message was deleted...");
ds.getRef().updateChildren(hashMap);
chatList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, chatList.size());
Toast.makeText(context, "message deleted...", Toast.LENGTH_SHORT).show();
}

Recyclerview load more lost item click when scrolling

I have a problem with Recyclerview item click. I fetch data to list() method and add via addItem() method in recyclerview custom adapter when scroll down in addOnScrollListener. I get item position with click interface on Fragment. First fetch data work perfectly but when fetch loadmore, can't retrive item positon to new data with onButtonLClick() method.
// in onBindViewHolder;
holder.lnl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
rcylviewItemNotify.onButtonLClick(position);
}catch (Throwable e){
//interface can be null
}
}
});
// addItem() method in adapter;
public void addItem(List<Image> img) {
for (Image im : img) {
arrayList.add(im);
}
notifyDataSetChanged();
}
// interface code;
public interface RcylviewItemNotify {
void onButtonLClick(int position);
}
// in Fragment code;
public void list() {
GetServices service = RetrofitInstance.getRetrofitInstance().create(GetServices.class);
Call<Images> call = service.getImages();
call.enqueue(new Callback<Images>() {
#Override
public void onResponse(Call<Images> call, Response<Images> response) {
Images body = response.body();
records = body.getImages();
adapter.addItem(records);
}
#Override
public void onFailure(Call<Images> call, Throwable t) {
Toast.makeText(getActivity(), "Network hatası onFailure", Toast.LENGTH_SHORT).show();
reflesh.setRefreshing(false);
}
});
}
#Override
public void onButtonLClick(int position) {
final String clickId = String.valueOf(records.get(position).getID());
Toast.makeText(getActivity(), "ID: " + clickId, Toast.LENGTH_SHORT).show();
}
// recycler settings;
public void loadView() {
layoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(layoutManager);
Collections.reverse(records);
adapter = new RecyclerViewAdapter(this,(ArrayList<Image>) records, getActivity());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
reflesh.setRefreshing(false);
}
I'm not sure if this is your issue but you should be using the ViewHolder to get the position. Inside of your onBindViewHolder:
#Override
public void onClick(View view){
int itemPosition = holder.getAdapterPosition();
// Then do whatever you need to with the position
}

Stop animation of Recyclerview when load more data

i'm tying to load more data each time user reached at the bottom of Recyclerview by get data of next page , using this :
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (!recyclerView.canScrollVertically(1) && isEnd) {
page++;
isEnd = false;
new fetchData().execute(page);
}
}
});
And everything works fine , but i have a problem with animation , each time i load more data Recyclerview disappears and shows.
here's my rest of code :
public class fetchData extends AsyncTask<Integer, List<Wallpaper>, List<Wallpaper>> {
Document doc = null;
ArrayList<Wallpaper> urls = new ArrayList<>();
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected List<Wallpaper> doInBackground(Integer... integers) {
try {
doc = Jsoup.connect(URLSource+"/page/" + integers[0]).get();
} catch (IOException e) {
e.printStackTrace();
}
Elements newsHeadlines = doc.select(".thumbnail");
for (Element headline : newsHeadlines) {
String thumb = headline.select("img").attr("src");
String title = headline.select("img").attr("alt");
Wallpaper wallpaperInfo = new Wallpaper();
URL = URL.replace("/wallpapers/", "/walls/");
wallpaperInfo.setThumbnails(URL);
wallpaperInfo.setTitle(title);
urls.add(wallpaperInfo);
}
return urls;
}
#Override
protected void onPostExecute(List<Wallpaper> wallpapers) {
super.onPostExecute(wallpapers);
setAdapterForRecyclerView(wallpapers);
isEnd = true;
}
}
private void setAdapterForRecyclerView(List<Wallpaper> wallpapers) {
if (myAdapter == null) {
myAdapter = new MyAdapter(wallpapers, getActivity(), new RecyclerViewClickListener() {
#Override
public void onClick(View view, Wallpaper wallpaper) {
Intent intent = new Intent(getActivity(), FullScreen.class);
intent.putExtra("img", wallpaper.getThumbnails());
if (wallpaper.getTitle().isEmpty()) {
intent.putExtra("title", "Unknown");
} else {
intent.putExtra("title", wallpaper.getTitle());
}
startActivity(intent);
/* TODO: FULLSCREEN ACTIVITY HERE*/
}
#Override
public void onClick(View view, Category categories) {
}
});
recyclerView.setAdapter(myAdapter);
} else {
myAdapter.getItems().addAll(wallpapers);
myAdapter.notifyDataSetChanged();
}
progressbar.setVisibility(View.GONE);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (!recyclerView.canScrollVertically(1) && isEnd) {
page++;
isEnd = false;
new fetchData().execute(page);
}
}
});
}
hope you guys help me to stop this animation , i just want to load more data without any animation
i think that your problem is in the calling of setAdapterForRecyclerView as there you may create new adapter instance and set this new adapter to the recyclerview again which lead to this behavior.
i recommend to append the new list of wallpapers objects to the recyclerview list then call notifydatasetchanged

Can't realize RecycleAdapter to custom ArrayList to get random query result on database

I have a database in Firestore and RecycleView to put the data from it. I send some query to Firestore and it get me the result depending on my request. But what, if I want to take 3 random results from the set issued to me on request. That is I get, for example, 20 results and want to take only 3 result each time when I send a request. And each time, it must be random 3 results from this 20?
I was helped to implement the mechanism of selection of 3 random results into the code, but I do not understand how to connect it to my adapter.
public class Myactivity extends AppCompatActivity {
public RecyclerView mResultList;
public FirebaseFirestore mFirestore;
public com.google.firebase.firestore.Query query;
public FirestoreRecyclerAdapter<Places, PlaceViewHolder> firestoreRecyclerAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle_activity);
mResultList = findViewById(R.id.list_result);
Boolean l_check1 = getIntent().getExtras().getBoolean("1");
Boolean l_check2 = getIntent().getExtras().getBoolean("2");
Boolean l_check3 = getIntent().getExtras().getBoolean("3");
mFirestore = FirebaseFirestore.getInstance();
if (l_check1) {
query = mFirestore.collection("Places").whereEqualTo("colour", "1").limit(20);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<Places> placesList = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
Places place = document.toObject(Places.class);
placesList.add(place);
}
int placeCount = placesList.size();
int randomNumber = new Random().nextInt(placeCount);
List<Places> randomPlaceList = new ArrayList<>();
for (int i=1; i<=3; i++) {
randomPlaceList.add(placesList.get(randomNumber));
}
}
}
});
} else if (l_check2) {
query = mFirestore.collection("Places").whereEqualTo("colour", "2").limit(3);
} else if (l_check3) {
query = mFirestore.collection("Places").whereEqualTo("colour", "3").limit(3);
}
mResultList.setLayoutManager(new LinearLayoutManager(this));
FirestoreRecyclerOptions<Places> options = new FirestoreRecyclerOptions.Builder<Places>()
.setQuery(query, Places.class)
.build();
firestoreRecyclerAdapter = new FirestoreRecyclerAdapter<Places, PlaceViewHolder>(options) {
#Override
protected void onBindViewHolder(PlaceViewHolder holder, int position, Places model) {
holder.setDetails(getApplicationContext(), model.getName(), model.getImage());
}
#Override
public PlaceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_layout, parent, false);
return new PlaceViewHolder(view);
}
};
mResultList.setAdapter(firestoreRecyclerAdapter);
}
#Override
protected void onStart() {
super.onStart();
firestoreRecyclerAdapter.startListening();
}
class PlaceViewHolder extends RecyclerView.ViewHolder {
View mView;
public PlaceViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setDetails(Context context, String placeName, String placeImage) {
final TextView place_Name = mView.findViewById(R.id.text_image_id);
ImageView place_Image = mView.findViewById(R.id.image_id);
place_Name.setText(placeName);
Glide.with(context).load(placeImage).into(place_Image);
place_Name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Card_activity.class);
intent.putExtra("qwerty", place_Name.getText());
startActivity(intent);
}
});
}
}
#Override
protected void onStop() {
super.onStop();
if (firestoreRecyclerAdapter != null) {
firestoreRecyclerAdapter.stopListening();
}
}
}
My Place.class:
public class Places {
private String image, name;
public Places() { }
public Places(String image, String name) {
this.image = image;
this.name = name;
}
public String getImage() { return image; }
public String getName() { return name; }
}
First of all, your logic for generating the randomPlaceList is incorrect as you just generate one Random Number using new Random().nextInt(placeCount) and then just keep using it thrice for adding entries to your list.
You should update your logic as follows,
...
final int placeCount = placesList.size();
final Random randomGenerator = new Random();
List<Places> randomPlaceList = new ArrayList<>();
for (int i = 0; i<=3; i++) {
randomPlaceList.add(placesList.get(randomGenerator.nextInt(placeCount)));
}
...
See this thread to see how you can fill the RecyclerView after you fetch the results from Firebase:
How to fill RecyclerView Adapter with Firebase

Categories

Resources