RecyclerView won't update after deleting file - android

I have CardViews in RecyclerView and each one has a delete button, when clicked it deletes data from database, remove item from ArrayList on that position and runs the animation.
When deleted it dissapears and creates a copy of it self ,Toast shows that ArrayList is empty/ without that item, but its still there I can click on it and everything, only after refreshing fragment(Changing to another fragment in bottom navigation tab or clicking on the same one) it dissapears.
This is my Adapter:
public class KitAdapter extends RecyclerView.Adapter<KitAdapter.MyViewHolder> {
Context context;
ArrayList<Kit> kitList;
OnKitClickListener clickListener;
FirebaseFirestore db;
FirebaseAuth mAuth;
FirebaseStorage FS;
String userID, saveShare;
ArrayList<String> DRList;
public KitAdapter(Context context, ArrayList<Kit> kitList, OnKitClickListener clickListener, String saveShare, ArrayList<String> DRList) {
this.context = context;
this.kitList = kitList;
this.clickListener = clickListener;
this.saveShare = saveShare;
this.DRList = DRList;
}
public ArrayList<Kit> getKitList() {
return kitList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.kitview, parent, false);
return new MyViewHolder(v, clickListener);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
Kit kit = kitList.get(position);
db = FirebaseFirestore.getInstance();
mAuth = FirebaseAuth.getInstance();
FS = FirebaseStorage.getInstance();
this.userID = kit.getUserID();
holder.Name.setText(kit.getName());
holder.kcal.setText(kit.getKcalM() / kit.getSavedDays() + "kcal");
holder.prot.setText(kit.getProtM() / kit.getSavedDays() + "g");
holder.uh.setText(kit.getUhM() / kit.getSavedDays() + "g");
holder.mast.setText(kit.getMastM() / kit.getSavedDays() + "g");
holder.numOfDays.setText("Broj dana " + kit.getSavedDays() + "");
holder.NameS = kit.getName();
holder.kcalS = String.valueOf(kit.getKcalM());
holder.protS = String.valueOf(kit.getProtM());
holder.uhS = String.valueOf(kit.getUhM());
holder.mastS = String.valueOf(kit.getMastM());
holder.dayNums = kit.getSavedDays();
holder.DRlist = this.DRList;
holder.userID = this.userID;
if(saveShare.equals("Saved")){
if(kit.getShared().equals("No")) {
holder.saveShare.setImageResource(R.drawable.ic_upload_black_24dp);
holder.saveShare.setTag(R.drawable.ic_upload_black_24dp);
}else{
holder.saveShare.setImageResource(R.drawable.ic_file_download_done_black_24dp);
holder.saveShare.setTag(R.drawable.ic_file_download_done_black_24dp);
}
if(!userID.equals(mAuth.getCurrentUser().getUid())){
holder.saveShare.setVisibility(View.GONE);
}
}else if(saveShare.equals("Search")){
holder.saveShare.setImageResource(R.drawable.ic_file_download_black_24dp);
holder.saveShare.setTag(R.drawable.ic_file_download_black_24dp);
holder.delete.setVisibility(View.GONE);
if(DRList.contains(holder.NameS)){
holder.saveShare.setImageResource(R.drawable.ic_file_download_done_black_24dp);
holder.saveShare.setTag(R.drawable.ic_file_download_done_black_24dp);
}
}
db.collection("Users Data")
.document(this.userID)
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
User user = task.getResult().toObject(User.class);
String userName = user.getUsername();
holder.userName.setText(userName);
}
});
FS.getReference().child(this.userID + ".jpg")
.getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(#NonNull Uri uri) {
Picasso.get().load(uri).into(holder.pfp);
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
holder.pfp.setImageResource(R.drawable.ic_person_black_24dp);
}
});
}
#Override
public int getItemCount() {
return kitList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView Name, numOfDays, kcal, prot, uh, mast, userName;
ImageView saveShare, delete;
String NameS, kcalS, protS, uhS, mastS, userID;
int dayNums;
OnKitClickListener onKitListener;
CircleImageView pfp;
ArrayList<String> DRlist;
public MyViewHolder(#NonNull View itemView, OnKitClickListener onKitListener) {
super(itemView);
Name = itemView.findViewById(R.id.kitName);
numOfDays = itemView.findViewById(R.id.dayNum);
kcal = itemView.findViewById(R.id.kcal);
prot = itemView.findViewById(R.id.prot);
uh = itemView.findViewById(R.id.uh);
mast = itemView.findViewById(R.id.mast);
userName = itemView.findViewById(R.id.userName);
pfp = itemView.findViewById(R.id.pfp);
saveShare = itemView.findViewById(R.id.saveShare);
delete = itemView.findViewById(R.id.delete);
this.onKitListener = onKitListener;
itemView.setOnClickListener(this);
saveShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onKitListener.onKitAdd(getAdapterPosition(), saveShare, Name.getText().toString());
}
});
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onKitListener.onKitDelete(getAdapterPosition(), (CardView) itemView, userID);
}
});
}
#Override
public void onClick(View v) {
onKitListener.onKitClick(getAdapterPosition(), NameS, kcalS, protS, uhS, mastS, dayNums);
}
}
public interface OnKitClickListener{
void onKitClick(int position, String NameS, String kcalS, String protS, String uhS, String mastS, int dayNums);
void onKitAdd(int position, ImageView addShare, String NameS);
void onKitDelete(int position, CardView card, String userID);
}
}
This is part of code in fragment that deletes item:
#Override
public void onKitDelete(int position, CardView card, String userID) {
Handler handler = new Handler();
if(userID.equals(mAuth.getCurrentUser().getUid())) {
db.collection("Users Data")
.document(userID)
.collection("Kits")
.whereEqualTo("name", kitlist.get(position).getName())
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (!task.isSuccessful()) {
return;
}
task.getResult().getDocuments().get(0).getReference()
.delete();
}
});
}else{
db.collection("Users Data")
.document(mAuth.getCurrentUser().getUid())
.collection("Saved Kits")
.document(kitlist.get(position).getName())
.delete();
}
Toast.makeText(getContext(), kitlist.toString(), Toast.LENGTH_SHORT).show();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.zoomout);
card.startAnimation(anim);
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(kitlist.isEmpty()){
emptTxt.setVisibility(View.VISIBLE);
kitlist.remove(position);
rec.removeViewAt(position);
adapter.notifyItemRemoved(position);
}
}
},300);
}
}, 150);
}
I tried these combinations:
kitlist.remove(position);
rec.removeViewAt(position);
adapter.notifyItemRemoved(position);
//This one you can see in code above
kitlist.remove(position);
adapter.notifyDataSetChange(position)
And tried making onKitDelete default and using notfiy()/notifyAll()
public interface OnKitClickListener{
void onKitClick(int position, String NameS, String kcalS, String protS, String uhS, String mastS, int dayNums);
void onKitAdd(int position, ImageView addShare, String NameS);
default void onKitDelete(int position, CardView card, String userID){
notify();
}
}
I always get same resoult.

that's easy to update your recycler view with
android ViewModel architechtureAndroid ViewModel Docs

Related

Recyclerview won't show items, Firebase Admin SDK, Realtime Database

I'm trying to retrieve some simple data from my database as an Admin, the data has been retrieved, but nothing is shown on the recyclerview.
In the other hand, i was trying to assign a value (String) from the database to a TextView, i was recieving the value, but the TextView was empty, i went to the .xml file and changed the layout_width from android:layout_width="wrap_content" to android:layout_width="match_parent" and it worked! Before that with the simple Firebase Client SDK it was showing the texte even with android:layout_width="wrap_content"
PS: Since i started using the Firebase Admin SDK, I didn't had any issues with writing data to the Realtime database, Or with creating Users. I tried both of them and they works fine.
Here's the retrieving function :
private void RetriveClasses() {
// Get a reference to our posts
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("Classrooms");
mclassrooms.clear();
// Attach a listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren())
{
Classroom post = snapshot.getValue(Classroom.class);
if (!post.getDeleted().equals("true"))
{
mclassrooms.add(post);
}
mAdapter.notifyDataSetChanged();
System.out.println(post.getClassName());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
}
The System.out.println(post.getClassName()); Shows :
I/System.out: L1 ST 2022-2023
L1 ST 2021-2022
L1 ST 2015-2016
These are the class names i'm looking for, which means the reading is succeed, but the recyclerview still won't show any items.
here's my Adapter:
public class ClassroomsAdapter extends RecyclerView.Adapter<ClassroomsAdapter.ViewHolder> {
private Context c;
public List<Classroom> mclassrooms;
public ClassroomsAdapter(List<Classroom> mclassrooms) {
this.mclassrooms = mclassrooms;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
this.c = parent.getContext();
View view = LayoutInflater.from(c).inflate(R.layout.classroom_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
final Classroom classroom = mclassrooms.get(position);
if (classroom.getClassName().length() >= 20)
{
String className = classroom.getClassName();
holder.classroom_name.setText(className.substring(0,19) + "...");
}
else
{
holder.classroom_name.setText(classroom.getClassName());
}
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(c, R.style.BottomSheetDialogTheme);
View eview = LayoutInflater.from(c).inflate(R.layout.bottom_menu_classroom, null);
eview.findViewById(R.id.Delete_Comment_Btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final ProgressDialog progressDialog = new ProgressDialog(c, R.style.MyAlertDialogStyle);
progressDialog.setTitle("Suppression");
progressDialog.setMessage("Traitement...");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
DatabaseReference mRoot = FirebaseDatabase.getInstance().getReference("Classrooms");
mRoot.child(classroom.getClassId()).child("deleted").setValue("true", new DatabaseReference.CompletionListener(){
#Override
public void onComplete(DatabaseError error, DatabaseReference ref) {
progressDialog.dismiss();
Toast.makeText(c, "Suppression avec succès!", Toast.LENGTH_SHORT).show();
bottomSheetDialog.dismiss();
}
});
}
});
eview.findViewById(R.id.copy_comment_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final BottomSheetDialog bs = new BottomSheetDialog(c, R.style.BottomSheetDialogTheme);
View aview = LayoutInflater.from(c).inflate(R.layout.edit_class_name, null);
EditText name_et = aview.findViewById(R.id.Edition_Comment_Input_Field);
name_et.setText(classroom.getClassName());
aview.findViewById(R.id.update_comment).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (name_et.getText().toString().isEmpty()) {
Toast.makeText(c, "Ce champ est requis*", Toast.LENGTH_SHORT).show();
} else {
final ProgressDialog progressDialog = new ProgressDialog(c, R.style.MyAlertDialogStyle);
progressDialog.setTitle("Mise à jour");
progressDialog.setMessage("Traitement...");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
DatabaseReference mRoot = FirebaseDatabase.getInstance().getReference("Classrooms");
mRoot.child(classroom.getClassId()).child("className").setValue(name_et.getText().toString(), new DatabaseReference.CompletionListener() {
#Override
public void onComplete(DatabaseError error, DatabaseReference ref) {
progressDialog.dismiss();
Toast.makeText(c, "Mise à jour avec succès!", Toast.LENGTH_SHORT).show();
bs.dismiss();
bottomSheetDialog.dismiss();
}
});
}
}
});
aview.findViewById(R.id.close_edit_comment).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bs.dismiss();
}
});
bs.setContentView(aview);
bs.show();
}
});
bottomSheetDialog.setContentView(eview);
bottomSheetDialog.show();
return false;
}
});
}
#Override
public int getItemCount() {
return mclassrooms.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView classroom_name;
public ViewHolder(#NonNull View itemView) {
super(itemView);
classroom_name = itemView.findViewById(R.id.class_name);
}
}
}
Thanks for your time.

NotifyDataSetChanged Recycler view : Cannot call this method while Recycler

I'm making a chat app. I have a recycler view. I use Picasso to download the images of the user and the friend he is chatting with. To not download the image for every message, when I download the first image I just put it into a bitmap and use it every time.
PROBLEM: I can't call NotifyDataSetChanged. I have tried every possible thing I've found on the net.
ERROR :
Cannot call this method while RecyclerView is computing a layout or scrolling android.support.v7.widget.RecyclerView
I have to "manually" refresh the view so that the images are loaded. No problem for the text.
It runs with the Handler but it doesn't even go in the function (so the view isn't refreshed).
Here is my code (everything works except this notifydatasetchanged)
public class ChatActivity extends AppCompatActivity {
private RecyclerView recyclerChat;
private ListMessageAdapter adapter;
private EditText editWriteMessage;
private LinearLayoutManager linearLayoutManager;
private FirebaseAuth mAuth;
private DatabaseReference UserRef, MessageIDREF, UserMessageRef, SendMessageUser;
private List<ChatClass> ListOfChats = new ArrayList<>();
private ImageButton btnSend;
private String currentUserID, currentDateMessage, currentTimeMessage;
private CommunActivit obj = new CommunActivit();
private String FRIENDID;
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
mAuth=FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
MessageIDREF = FirebaseDatabase.getInstance().getReference().child("Friends");
UserMessageRef = FirebaseDatabase.getInstance().getReference().child("Message").child(obj.getsIDMESSAGE());
btnSend = (ImageButton) findViewById(R.id.btnSend);
editWriteMessage = (EditText) findViewById(R.id.editWriteMessage) ;
FRIENDID = obj.getsIDFRIEND();
mToolbar = (Toolbar) findViewById(R.id.main_app_bar2);
setSupportActionBar(mToolbar);
ActionBar ab = getSupportActionBar();
final String Title = obj.getsFRIENDName();
ab.setTitle(Title);
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SendMessageToGroupChat();
editWriteMessage.setText("");
}
});
retrievedata();
/*recyclerChat = (RecyclerView)findViewById(R.id.recyclerChat);
adapter = new ListMessageAdapter(this,ListOfChats);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerChat.setLayoutManager(layoutManager);
recyclerChat.setItemAnimator(new DefaultItemAnimator());
recyclerChat.setAdapter(adapter);*/
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerChat = (RecyclerView) findViewById(R.id.recyclerChat);
recyclerChat.setLayoutManager(linearLayoutManager);
adapter = new ListMessageAdapter(this, ListOfChats);
recyclerChat.setAdapter(adapter);
/* editWriteMessage.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
recyclerChat.smoothScrollToPosition(adapter.getItemCount());
return false;
}
});*/
}
private void SendMessageToGroupChat() {
String message = editWriteMessage.getText().toString();
String messageKey = UserMessageRef.push().getKey();
if (TextUtils.isEmpty(message)) {
Toast.makeText(this, "Please write a message first", Toast.LENGTH_SHORT).show();
} else {
Calendar calendarDate = Calendar.getInstance();
SimpleDateFormat DateFormat = new SimpleDateFormat("MMM dd, yyyy");
currentDateMessage = DateFormat.format(calendarDate.getTime());
Calendar calendarTime = Calendar.getInstance();
SimpleDateFormat TimeFormat = new SimpleDateFormat("hh:mm a");
currentTimeMessage = TimeFormat.format(calendarTime.getTime());
HashMap<String, Object> groupMessageKey = new HashMap<>();
UserMessageRef.updateChildren(groupMessageKey);
SendMessageUser = UserMessageRef.child(messageKey);
HashMap<String, Object> messageInfoMap = new HashMap<>();
messageInfoMap.put("name", obj.getsUsername());
messageInfoMap.put("message", message);
messageInfoMap.put("date", currentDateMessage);
messageInfoMap.put("time", currentTimeMessage);
messageInfoMap.put("image", obj.getsUserImageLink());
SendMessageUser.updateChildren(messageInfoMap);
}
}
public void retrievedata() {
UserMessageRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) /**** HERE IS THE PROBLEM***/
{
if (dataSnapshot.exists()) {
DisplayMessages(dataSnapshot);
}
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
if (dataSnapshot.exists()) {
DisplayMessages(dataSnapshot);
}
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void DisplayMessages(DataSnapshot dataSnapshot)
{
Iterator iterator = dataSnapshot.getChildren().iterator();
while (iterator.hasNext()) {
String chatDate = (String) ((DataSnapshot) iterator.next()).getValue();
String chatImage = (String) ((DataSnapshot) iterator.next()).getValue();
String chatMessage = (String) ((DataSnapshot) iterator.next()).getValue();
String chatName = (String) ((DataSnapshot) iterator.next()).getValue();
String chatTime = (String) ((DataSnapshot) iterator.next()).getValue();
ListOfChats.add(new ChatClass(chatName, chatMessage, chatDate + " " + chatTime, chatImage));
adapter.notifyDataSetChanged();
recyclerChat.smoothScrollToPosition(adapter.getItemCount()); /**AUTO SCROLL**/
}
}
class ListMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private ChatClass consersation;
private List<ChatClass> List_of_chats;
private CommunActivit obj = new CommunActivit();
private Bitmap BitmapUser;
private Bitmap BitmapFriend;
public ListMessageAdapter(Context context, List<ChatClass> List_of_chats) {
this.context = context;
this.List_of_chats = List_of_chats;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 1) {
View view = LayoutInflater.from(context).inflate(R.layout.rc_item_message_friend, parent, false);
return new ItemMessageFriendHolder(view);
} else if (viewType == 2) {
View view = LayoutInflater.from(context).inflate(R.layout.rc_item_message_user, parent, false);
return new ItemMessageUserHolder(view);
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final int pos =position;
if (holder instanceof ItemMessageFriendHolder) {
((ItemMessageFriendHolder) holder).txtContent.setText(List_of_chats.get(position).getMessage());
if (BitmapFriend == null) {
Picasso.get().load(List_of_chats.get(position).getImageLink()).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
BitmapFriend = bitmap;
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
((ItemMessageFriendHolder) holder).avata.setImageBitmap(BitmapFriend);
}
else
{
((ItemMessageFriendHolder) holder).avata.setImageBitmap(BitmapFriend);
new Handler().post(new Runnable() {
#Override
public void run() {
notifyDataSetChanged();
}
});
}
}
else
{
if (holder instanceof ItemMessageUserHolder) {
((ItemMessageUserHolder) holder).txtContent.setText(List_of_chats.get(position).getMessage());
if (BitmapUser == null) {
Picasso.get().load(List_of_chats.get(position).getImageLink()).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
BitmapUser = bitmap;
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
((ItemMessageUserHolder) holder).avata.setImageBitmap(BitmapUser);
} else {
((ItemMessageUserHolder) holder).avata.setImageBitmap(BitmapUser);
new Handler().post(new Runnable() {
#Override
public void run() {
notifyDataSetChanged();
}
});
}
}
}
}
#Override
public int getItemViewType(int position) {
int viewtype = -1;
if (List_of_chats.get(position).getUsername().equals(obj.getsUsername())) {
return 2;
}
else
{
return 1;
}
}
#Override
public int getItemCount() {
return List_of_chats.size();
}
}
class ItemMessageUserHolder extends RecyclerView.ViewHolder {
public TextView txtContent;
public CircleImageView avata;
public ItemMessageUserHolder(View itemView) {
super(itemView);
txtContent = (TextView) itemView.findViewById(R.id.textContentUser);
avata = (CircleImageView) itemView.findViewById(R.id.imageView2);
}
}
class ItemMessageFriendHolder extends RecyclerView.ViewHolder {
public TextView txtContent;
public CircleImageView avata;
public ItemMessageFriendHolder(View itemView) {
super(itemView);
txtContent = (TextView) itemView.findViewById(R.id.textContentFriend);
avata = (CircleImageView) itemView.findViewById(R.id.imageView3);
}
}
It is because you're calling notifyDataSetChanged inside your onBindViewHolder like this:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
...
((ItemMessageFriendHolder) holder).avata.setImageBitmap(BitmapFriend);
new Handler().post(new Runnable() {
#Override
public void run() {
notifyDataSetChanged();
}
});
...
}
You shouldn't force the Adapter to refresh itself before it isn't finished binding the view yet.
So, you need to remove the following code from your onBindViewHolder:
new Handler().post(new Runnable() {
#Override
public void run() {
notifyDataSetChanged();
}
});
then just call notifyDataSetChanged() when you need it. For example, when your dataset is changed.
Try this.
dialeecyclerView.post(new Runnable()
{
#Override
public void run() {
myadapter.notifyDataSetChanged();
}
});
Source - Cannot call this method while RecyclerView is computing a layout or scrolling when try remove item from recyclerview

get key position in onBindViewHolder firebase database

I want to get key position in database.
getting error: getRef(position) : Cannot resolve method 'getRef(int)' on
final String post_key = getRef(position).getKey();
this is my code CustomPostAdapter
public class CustomPostAdapter extends RecyclerView.Adapter<CustomPostAdapter.ViewHolder>{
public List<Spacecraft> userList ;
public Context context ;
private boolean mProcessLike = false ;
private DatabaseReference mDatabaseLike ;
// public ImageView imagePostt ;
public CustomPostAdapter(Context context , List<Spacecraft> userList){
this.userList = userList ;
this.context = context;
}
#NonNull
#Override
public CustomPostAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_posts_item, parent,false);
return new CustomPostAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
mDatabaseLike = FirebaseDatabase.getInstance().getReference().child("Likes");
mDatabaseLike.keepSynced(true);
final String post_key = getRef(position).getKey();
final String name = userList.get(position).getName();
final String image_post = userList.get(position).getImage_post();
final String descrip = userList.get(position).getDescrip();
final String hachatg = userList.get(position).getHachtag();
final String user = userList.get(position).getUser();
final String time = userList.get(position).getTime();
//Text Post
holder.nameText.setText(name);
holder.descripText.setText(descrip);
holder.hachtagText.setText(" " + hachatg);
holder.timeText.setText(" " + time);
//Imgae Post if null set Visibtly Gone to ImageView
if (TextUtils.isEmpty(image_post)){
holder.imagePostt.setVisibility(View.GONE);
}else {
//Image Post
Picasso.with(context).load(image_post).into(holder.imagePostt , new Callback() {
#Override
public void onSuccess() {
holder.imagePostt.setVisibility(View.VISIBLE);
}
#Override
public void onError() {
holder.imagePostt.setVisibility(View.GONE);
}
});
}
//Onclick items
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context , ContentActivity.class);
intent.putExtra("NAME_KEY", name);
intent.putExtra("HACHTAG_KEY", hachatg);
intent.putExtra("DESCRIP_KEY", descrip);
intent.putExtra("USER_KEY", user);
intent.putExtra("IMAGE_POST", image_post);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
//Button Like Onclice
holder.mLikeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mProcessLike = true;
if (mProcessLike){
mDatabaseLike.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Chek if user already Like or not
if (dataSnapshot.child(post_key).hasChild(user)){
}else {
mDatabaseLike.child(post_key).child(user).setValue("RandomValue");
mProcessLike = false;
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
});
}
#Override
public int getItemCount() {
return userList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
View mView;
public TextView nameText , hachtagText , descripText, urlText , timeText;
public ImageView imagePostt ;
public ImageButton mLikeBtn ;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
nameText = (TextView)mView.findViewById(R.id.text_nameProblem);
hachtagText = (TextView)mView.findViewById(R.id.text_nameUser);
// urlText = (TextView)mView.findViewById(R.id.textUrl);
descripText = (TextView)mView.findViewById(R.id.text_Discription);
timeText = (TextView)mView.findViewById(R.id.timeText);
imagePostt = (ImageView)mView.findViewById(R.id.imagePost);
mLikeBtn = (ImageButton)mView.findViewById(R.id.btnLike);
}
}
}
You should be using FirebaseRecyclerAdapter which has the method getRef(). follow this link to learn how to use it
There are two ways in which you can solve this. The first one would be to store the key as a property in your user object and use:
final String key = userList.get(position).getKey();
And the second one would be to simply use FirebaseRecyclerAdapter instead and call:
mFirebaseRecyclerAdapter.getKey(position);
As explained here by #Puf.

load image and video in recylerview

I have an adapter class where I am able to load all video from Firebase. But the problem is I can't load both, image and video in one RecyclerView
See what I have done:
I am using toro library to auto play video if your wondering and ExoPlayer to play video
This is my adapter class
public class MainFeedAdapter extends RecyclerView.Adapter<TestAdapter.MyViewHolder> {
private static final String TAG = "MainfeedAdapter";
private List<Photo> moviesList;
private DatabaseReference mReference;
private Context mContext;
private String currentUsername = "";
private int mLayoutResource;
private LayoutInflater mInflater;
private Photo photo;
private MyViewHolder mHolder;
public class MyViewHolder extends RecyclerView.ViewHolder implements ToroPlayer{
static final int LAYOUT_RES = R.layout.main_list;
private ExoPlayerViewHelper helper;
private CircleImageView profile_image;
//private String likeString;
private TextView username,time,caption,likes,comment,stars;
private SquareImageView image;
private LikeButton mHeart,Star;
private UserAccountSettings userAccountSettings = new UserAccountSettings();
private User user = new User();
private StringBuilder users;
private String mLIkeString;
private String mStarString;
private boolean likeByCurrentUSer;
private boolean starbycurrentuser;
private DatabaseReference mNotification;
private ImageView commentBubble;
private Uri mediaUri;
#BindView(R.id.main_post_image2)
PlayerView playerView;
private CardView video;
public MyViewHolder(View view) {
super(view);
profile_image = (CircleImageView)view.findViewById(R.id.post_profile_photo);
username = (TextView) view.findViewById(R.id.main_username);
time = (TextView) view.findViewById(R.id.main_image_time_posted);
caption = (TextView) view.findViewById(R.id.main_image_caption);
likes = (TextView) view.findViewById(R.id.main_likes);
comment = (TextView) view.findViewById(R.id.main_showcomments);
image = (SquareImageView) view.findViewById(R.id.main_post_image);
mHeart = (LikeButton) view.findViewById(R.id.main_heart);
Star = (LikeButton) view.findViewById(R.id.main_star);
stars= (TextView)view.findViewById(R.id.stars);
commentBubble = (ImageView)view.findViewById(R.id.main_comments) ;
mNotification = FirebaseDatabase.getInstance().getReference().child("notification");
mReference = FirebaseDatabase.getInstance().getReference();
ButterKnife.bind(this, itemView);
video = (CardView)view.findViewById(R.id.video_posts);
}
#NonNull
#Override
public View getPlayerView() {
return playerView;
}
#NonNull
#Override
public PlaybackInfo getCurrentPlaybackInfo() {
return helper != null ? helper.getLatestPlaybackInfo() : new PlaybackInfo();
}
#Override
public void initialize(#NonNull Container container, #Nullable PlaybackInfo playbackInfo) {
if (helper == null) {
helper = new SimpleExoPlayerViewHelper(container, this, mediaUri);
}
helper.initialize(playbackInfo);
}
// called from Adapter to setup the media
void bind( Uri item, List<Photo> payloads) {
if (item != null) {
mediaUri = item;
}else {
video.setVisibility(View.GONE);
}
}
#Override
public void play() {
if (helper != null) helper.play();
}
#Override
public void pause() {
if (helper != null) helper.pause();
}
#Override
public boolean isPlaying() {
return helper != null && helper.isPlaying();
}
#Override
public void release() {
if (helper != null) {
helper.release();
helper = null;
}
}
#Override
public boolean wantsToPlay() {
return ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= 0.85;
}
#Override
public int getPlayerOrder() {
return getAdapterPosition();
}
#Override
public void onSettled(Container container) {
}
}
public TestAdapter(List<Photo> moviesList) {
this.moviesList = moviesList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.main_list, parent, false);
mContext = parent.getContext();
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
photo = moviesList.get(position);
mHolder = holder;
holder.users = new StringBuilder();
getCurrentUsername();
getLikesString(mHolder);
getStarString(mHolder);
holder.bind(Uri.parse(photo.getVideo_path()),moviesList);
//get the profile image and username
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_user_account_settings))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(getItem(position).getUser_id());
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
// currentUsername = singleSnapshot.getValue(UserAccountSettings.class).getUsername();
Log.d(TAG, "onDataChange: found user: "
+ singleSnapshot.getValue(UserAccountSettings.class).getUsername());
holder.username.setText(singleSnapshot.getValue(UserAccountSettings.class).getUsername());
holder.username.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: navigating to profile of: " +
holder.user.getUsername());
Intent intent = new Intent(mContext, Profile_Activity.class);
intent.putExtra(mContext.getString(R.string.calling_activity),
mContext.getString(R.string.home_activity));
intent.putExtra(mContext.getString(R.string.intent_user), holder.user);
mContext.startActivity(intent);
}
});
imageLoader.displayImage(singleSnapshot.getValue(UserAccountSettings.class).getProfile_photo(),
holder.profile_image);
holder.profile_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: navigating to profile of: " +
holder.user.getUsername());
Intent intent = new Intent(mContext, Profile_Activity.class);
intent.putExtra(mContext.getString(R.string.calling_activity),
mContext.getString(R.string.home_activity));
intent.putExtra(mContext.getString(R.string.intent_user), holder.user);
mContext.startActivity(intent);
}
});
holder.userAccountSettings = singleSnapshot.getValue(UserAccountSettings.class);
holder.comment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((HomeActivity)mContext).onCommentThreadSelected(getItem(position),mContext.getString(R.string.home_activity));
//another thing?
((HomeActivity)mContext).hideLayout();
}
});
holder.commentBubble.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((HomeActivity)mContext).onCommentThreadSelected(getItem(position),mContext.getString(R.string.home_activity));
//another thing?
((HomeActivity)mContext).hideLayout();
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Query userQuery = mReference
.child(mContext.getString(R.string.dbname_users))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(getItem(position).getUser_id());
userQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Log.d(TAG, "onDataChange: found user: " +
singleSnapshot.getValue(User.class).getUsername());
holder.user = singleSnapshot.getValue(User.class);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private boolean rechedendoflist(int position){
return position == getItemCount() -1;\
}
#Override
public int getItemCount() {
return moviesList.size();
}
public Photo getItem(int position) {
return moviesList.get(position);
}
private void getCurrentUsername(){
Log.d(TAG, "getCurrentUser: ");
Log.d(TAG, "getCurrentUsername: retrieving user account settings");
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_users))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
currentUsername = singleSnapshot.getValue(UserAccountSettings.class).getUsername();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Please help me guys!. Thanks in Advance!.
TheRecyclerView has ability to show items in multiple types.
In your case you can define two ViewType. one for Images and another for Videos.
Search about RecyclerView with multiple view types.
Take a look at here and this.
Also here is a question and answers about it.

RecylerView Adapter in android Firebase is not quite syncing

I am creating a program, where user can update post, and other user and same user can react there. For that, I have implemented the love button. So this works this way, I have saved a unique id in the DatabaseReference and other reference for uniquekey for easiness. So, whenever, the user clicks on love button, it must update the total reaction of the node by increasing it to one. The problem I am getting is, whenever I press the love button key, it doesn't call the uniqueID I need. The uniqueId I am getting from model class from viewholder is not the post I have reacted to.
public class LatestFragment extends Fragment {
private RecyclerView latestFragmentRecyclerView;
private DatabaseReference mDatabaseReference;
private FirebaseAuth mAuth;
private static Context context;
private static boolean firstTime;
private static String totalVotes;
private static String uniqueKey;
private static DatabaseReference reactionDatabaseReference;
private static DatabaseReference uniqueKeysReferences;
private static Boolean userReactState;
private static List<String> allUniqueKeys;
public LatestFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_latest,container,false);
latestFragmentRecyclerView = (RecyclerView) v.findViewById(R.id.recyclerViewForLatestFragment);
latestFragmentRecyclerView.setHasFixedSize(true);
userReactState = false;
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
latestFragmentRecyclerView.setLayoutManager(layoutManager);
mAuth = FirebaseAuth.getInstance();
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Posts");
reactionDatabaseReference=FirebaseDatabase.getInstance().getReference().child("PostReactions");
uniqueKeysReferences = FirebaseDatabase.getInstance().getReference().child("UniqueKeys");
mDatabaseReference.keepSynced(true);
firstTime = true;
return v;
}
#Override
public void onStart() {
super.onStart();
final FirebaseRecyclerAdapter<Posts, PostViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Posts, PostViewHolder>(
Posts.class, R.layout.each_post_layout, PostViewHolder.class, mDatabaseReference
) {
#Override
protected void populateViewHolder(final PostViewHolder viewHolder,final Posts model, int position) {
Context c = getActivity();
context = c;
uniqueKey = model.getUnique();
viewHolder.setUsername(model.getUsername());
viewHolder.setCaption(model.getCaption());
viewHolder.setTime(model.getTime());
viewHolder.setImage(model.getImage(),c);
viewHolder.setCurrentUserImage(model.getUserPhoto(),c);
viewHolder.imageViewIfUserClicked(model.getCurrentUserReaction(),c);
totalVotes = model.getTotalReactions();
}
};
latestFragmentRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class PostViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
View theRecyclingView;
private Button loveButton ;
public PostViewHolder(View itemView) {
super(itemView);
theRecyclingView = itemView;
loveButton = (Button) theRecyclingView.findViewById(R.id.loveButton);
loveButton.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if(view.getId()==loveButton.getId())
{
allUniqueKeys = new ArrayList<>();
int position = getAdapterPosition();
Log.e("UniqueKey", String.valueOf(position));
uniqueKeysReferences.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot x : dataSnapshot.getChildren()) {
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
if(firstTime==true)
{
loveButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like_button_red,0,0,0);
firstTime = false;
int vote= Integer.valueOf(totalVotes);
vote++;
totalVotes = String.valueOf(vote);
reactionDatabaseReference.child(uniqueKey).child("TotalReactions").setValue(totalVotes).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful())
{
reactionDatabaseReference.child(uniqueKey).child("CurrentUserReaction").setValue("true").addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful())
{
}
else
{
Toast.makeText(context," Some Error Occured ",Toast.LENGTH_LONG).show();
}
}
});
}
else
{
Toast.makeText(context," Some Error Occured ",Toast.LENGTH_LONG).show();
}
}
});
}
else
{
loveButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like_button,0,0,0);
firstTime = true;
int vote= Integer.valueOf(totalVotes);
vote--;
totalVotes = String.valueOf(vote);
reactionDatabaseReference.child(uniqueKey).child("TotalReactions").setValue(totalVotes).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful())
{
reactionDatabaseReference.child(uniqueKey).child("CurrentUserReaction").setValue("false").addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful())
{
}
else
{
Toast.makeText(context," Some Error Occured ",Toast.LENGTH_LONG).show();
}
}
});
}
else
{
Toast.makeText(context," Some Error Occured ",Toast.LENGTH_LONG).show();
}
}
});
}
}
}
public void setUsername(String username)
{
TextView mUsername= (TextView) theRecyclingView.findViewById(R.id.userWhoPostedUserName);
mUsername.setText(username);
}
public void setCaption(String caption) {
TextView mUsername= (TextView) theRecyclingView.findViewById(R.id.captionForPic);
mUsername.setText(caption);
}
public void setImage(final String image, final Context c) {
final ImageView imageView = (ImageView) theRecyclingView.findViewById(R.id.userUploadedImageView);
Picasso.with(c).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(imageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(c).load(image).into(imageView);
}
});
}
public void setTime(String time) {
TextView time_of_posting = (TextView) theRecyclingView.findViewById(R.id.timeOfPosting_each_post_layout);
time_of_posting.setText(time);
}
public void setCurrentUserImage(final String userPhoto, final Context c) {
final CircleImageView imageView = (CircleImageView) theRecyclingView.findViewById(R.id.userWhoPostedCircleImageView);
Picasso.with(c).load(userPhoto).networkPolicy(NetworkPolicy.OFFLINE).into(imageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(c).load(userPhoto).into(imageView);
}
});
}
public void imageViewIfUserClicked(String currentUserReaction, final Context c) {
//reactionDatabaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
// #Override
//public void onDataChange(DataSnapshot dataSnapshot) {
// String CurrentuserReaction = dataSnapshot.child(uniqueKey).child("TotalReactions").child("CurrentUserReaction").getValue().toString();
// userReactState = Boolean.parseBoolean(CurrentuserReaction);
//}
//#Override
//public void onCancelled(DatabaseError databaseError) {
//
// }
// });
if(userReactState==false) {
loveButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like_button,0,0,0);
}
else
{
loveButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like_button_red,0,0,0);
}
}
}
}

Categories

Resources