Reputation: 567
I have a recycle view, 2 differents adapter and I have a default Image. The problem is that when a user doesn't have an image, sometimes it loads an other user image instead of the default image. (I also check if user doesn't have imagelink, it reset the default image programatically... so it should work => no problem for the text etc, only the image!)
Here is the xml code for one adapter (pretty much the same for the other):
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rella_rc_item_friend"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="8dp"
card_view:cardBackgroundColor="@color/colorCardView"
card_view:cardCornerRadius="5dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
card_view:cardBackgroundColor="@color/grey_200"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/icon_avata"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:src="@drawable/default_avata"
android:layout_weight="1"
app:civ_border_color="@color/colorPrimary"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_weight="5"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity=""
android:layout_weight="1"
android:orientation="horizontal"
android:paddingTop="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txtName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_toLeftOf="@id/txtTime"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtTime"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="match_parent"
android:gravity="center_vertical|right"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity=""
android:layout_weight="1">
<TextView
android:id="@+id/txtMessage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center_vertical"
android:lines="1"
android:paddingBottom="10dp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
class ListContactAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public List<MessageInfo> ListContacts;
private Context context;
FirebaseAuth mAuth;
String currentuserid;
SimpleDateFormat DateFormat = new SimpleDateFormat("dd MMMM, yyyy");
SimpleDateFormat TimeFormat = new SimpleDateFormat("hh:mm a");
SimpleDateFormat YearFormat = new SimpleDateFormat("yyyy");
SimpleDateFormat DayFormat = new SimpleDateFormat("MMMM dd, hh:mm a");
FusionProject Fobj = new FusionProject();
DatabaseReference VuRef;
CommunActivit obj = new CommunActivit();
public ListContactAdapter(Context context,List<MessageInfo> listGroup){
this.context = context;
this.ListContacts = listGroup;
mAuth = FirebaseAuth.getInstance();
currentuserid = mAuth.getUid();
VuRef = FirebaseDatabase.getInstance().getReference().child("Seen");
}
public ListContactAdapter ()
{
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 1) {
View view = LayoutInflater.from(context).inflate(R.layout.rc_item_contact_me, parent, false);
return new ItemForMe(view);
} else if (viewType == 2) {
View view = LayoutInflater.from(context).inflate(R.layout.rc_item_friend, parent, false);
return new ItemForMyFriend(view);
}
return null;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position)
{
String username = ListContacts.get(position).getFriendUsername();
String last_message = ListContacts.get(position).getLastMessage();
String time = ListContacts.get(position).getTime() ;
String image = ListContacts.get(position).getFriendPicture();
String FriendID = ListContacts.get(position).getFriendID();
Date Today = new Date(System.currentTimeMillis());
long yourmilliseconds = (Long.parseLong(time));
Date resultdate = new Date(yourmilliseconds);
String test = DateFormat.format(resultdate).toString(); /**DATE**/
String test2 = TimeFormat.format(resultdate).toString(); /**TIME**/
String test3 = YearFormat.format(resultdate);
String test4 = DayFormat.format(resultdate);
if (holder instanceof ItemForMe)
{
((ItemForMe) holder).username_friend.setText(username);
((ItemForMe) holder).last_message.setText("Me : " +last_message);
if (DateFormat.format(Today).equals(test))
{
((ItemForMe) holder).time.setText(test2);
}
else
{
if (YearFormat.format(Today).equals(test3))
{
String cap = test4.substring(0, 1).toUpperCase() + test4.substring(1);
((ItemForMe) holder).time.setText(cap);
}
else {
((ItemForMe) holder).time.setText(test);
}
}
if (!image.equals("")) {
Picasso.get().load(image).into(((ItemForMe) holder).Profil_friend);
}
else
{
((ItemForMe) holder).Profil_friend.setImageResource(R.drawable.default_avata);
}
VuRef.child(ListContacts.get(position).getIDMessage()).addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
if (dataSnapshot.exists())
{
if (dataSnapshot.getValue().toString().equals("Vu"))
{
((ItemForMe) holder).imageView_vu.setImageDrawable(holder.itemView.getContext().getResources().getDrawable(R.drawable.icons8_seen));
}
else
{
((ItemForMe) holder).imageView_vu.setImageDrawable(holder.itemView.getContext().getResources().getDrawable(R.drawable.icons8_not_vu));
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
else
{
((ItemForMyFriend) holder).username_friend.setText(username);
((ItemForMyFriend) holder).last_message.setText(last_message);
if (DateFormat.format(Today).equals(test))
{
((ItemForMyFriend) holder).time.setText(test2);
}
else
{
if (YearFormat.format(Today).equals(test3))
{
String cap = test4.substring(0, 1).toUpperCase() + test4.substring(1);
((ItemForMyFriend) holder).time.setText(cap);
}
else {
((ItemForMyFriend) holder).time.setText(test);
}
}
if (!image.equals("")) {
Picasso.get().load(image).into(((ItemForMyFriend) holder).Profil_friend);
}
else
{
((ItemForMyFriend) holder).Profil_friend.setImageResource(R.drawable.default_avata);
}
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent groupeIntent = new Intent(v.getContext(), ChatActivity.class);
Fobj.writefile("FriendID",ListContacts.get(position).getFriendID(), context);
Fobj.writefile("MessageID",ListContacts.get(position).getIDMessage(), context);
Fobj.writefile("FriendName",ListContacts.get(position).getFriendUsername(), context);
Fobj.writefile("FriendPicture",ListContacts.get(position).getFriendPicture(), context);
obj.setsIDFRIEND(ListContacts.get(position).getFriendID());
obj.setsIDMESSAGE(ListContacts.get(position).getIDMessage());
obj.setsFRIENDName(ListContacts.get(position).getFriendUsername());
context.startActivity(groupeIntent);
}
});
}
@Override
public int getItemViewType(int position) {
String test = ListContacts.get(position).getSenderID();
if (test.equals(currentuserid))
{
return 1;
}
else
{
return 2;
}
}
@Override
public int getItemCount() {
return ListContacts.size();
}
}
Retrieve data :
List_of_friends.add(0,new MessageInfo(ID, newgroup, profilepicture,MessageID, Date, Message, Time, SenderID));
adapter.notifyDataSetChanged();
So as you can see on this image, the first message wasn't loaded correctly. The user doesn't have an image but get the image user of one other user...
If anyone can help, would be nice !
I have debugged and it seems that it sometimes go more times in the holder than in the listsize.
Upvotes: 4
Views: 1526
Reputation: 1795
You can try with Glide like this.
Add this in app.gradle file
implementation "com.github.bumptech.glide:glide:4.8.0"
annotationProcessor "com.github.bumptech.glide:compiler:4.8.0"
Replace Picasso with glide like this
if (!image.trim().isEmpty())
Glide.with(context).load(image).into(((ItemForMyFriend) holder).Profil_friend);
Upvotes: 1
Reputation: 1795
Try loading image like this in your recyclerview
if (!image.trim().isEmpty())
Picasso.with(context).load(image)
.error(R.drawable.default_avata)
.placeholder(R.drawable.default_avata)
.into(((ItemForMyFriend) holder).Profil_friend);
Upvotes: 1