Reputation: 3274
I have some messages with different schema as
{
"messages" : {
"-Ka81zxkKhzi9tTMy3W3" : {
"messagePhoto" : "https://firebasestorage.googleapis.com/v0/b/MY_APP.appspot.com/o/url_to_image",
"photoUrl" : "http://pbs.twimg.com/profile_images/666473645494112256/pvJyunCa_normal.png",
"title" : "Arshad Ali Soomro",
"uid" : "USER_ID"
},
"-Ka822evCxQ90EQbfUlQ" : {
"message" : "Hi there",
"photoUrl" : "http://pbs.twimg.com/profile_images/666473645494112256/pvJyunCa_normal.png",
"title" : "Arshad Ali Soomro",
"uid" : "USER_ID"
},
"-Ka82I097uMiD2W1Kwm0" : {
"message" : "Good Evening every buddy",
"photoUrl" : "https://lh6.googleusercontent.com/-jSaki6d2OxI/AAAAAAAAAAI/AAAAAAAAAAo/2xwxxl-DEFE/s96-c/photo.jpg",
"title" : "Arslan Khan Soomro",
"uid" : "USER_ID"
}
}
}
I have this strange behavior of RecyclerView adapter look
a custom RecyclerView which can show text or image according to logic my xml layout is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="start"
android:orientation="horizontal" android:visibility="visible" android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/them_avatar_spacer"
android:layout_width="@dimen/avatar_size"
android:layout_height="0.0dip"
android:visibility="gone"/>
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@id/avatar_iv"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_email_account"
app:civ_border_width="2dp"
app:civ_border_color="#c6c1c2"
android:layout_margin="5dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4.0dip"
android:gravity="start"
android:orientation="vertical">
<TextView
android:id="@id/chat_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2.0dip"
android:layout_marginLeft="6.0dip"
android:layout_marginTop="6dp"
android:includeFontPadding="false"
android:text="Title"
android:textColor="@color/black_light"
android:textSize="@dimen/chat_name_fontsize"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:layout_marginRight="75.0dip"
android:background="@drawable/selectable_balloon_left"
android:orientation="vertical">
<ImageView android:id="@id/chat_msg_img"
android:layout_width="250dp"
android:layout_height="250dp"
android:visibility="gone"/>
<TextView
android:id="@id/message_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:clickable="true"
android:lineSpacingExtra="@dimen/text_body_line_spacing"
android:linksClickable="true"
android:text="@string/every_message_text_comes_here"
android:textColor="@color/black_heavy"
android:textIsSelectable="false"
android:textSize="@dimen/chat_text_msg_fontsize"/>
</LinearLayout>
</LinearLayout>
<TextView
android:id="@id/sent_at_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="12:15"
android:visibility="gone"
android:textColor="@color/black_lightest"
android:textSize="@dimen/chat_timestamp_fontsize" />
</LinearLayout>
in adapter the onBindViewHolder() is
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
MessageHolder messageHolder = (MessageHolder) holder;
try{
if(mMessageList.get(position).getPhotoUrl().equals("")){
messageHolder.mImageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_email_account));
} else {
Glide.with(mContext)
.load(mMessageList.get(position).getPhotoUrl())
.centerCrop()
.placeholder(R.drawable.ic_email_account)
.crossFade()
.into(messageHolder.mImageView);
}
if (isPhoto){
Glide.with(mContext)
.load(mMessageList.get(position).getMessagePhoto())
.centerCrop()
.placeholder(R.drawable.ic_choose_image)
.crossFade()
.into(messageHolder.mPhotoMessage);
}
} catch (Exception e){
Log.e("ADT 3", "Some thing went wrong...");
Log.e("ADT ERR", "Cause is " + e.getMessage() + "\n\n" + e.getCause());
}
messageHolder.mTitleTextView.setText(mMessageList.get(position).getTitle());
messageHolder.mMessageTextView.setText(mMessageList.get(position).getMessage());
try{
if (!mMessageList.get(position).getMessagePhoto().equals("")){
Log.e("PHOTO_TAG", "Yes there is photo");
messageHolder.mChatMsgImg.setVisibility(View.VISIBLE);
Glide.with(mContext)
.load(mMessageList.get(position).getMessagePhoto())
.centerCrop()
.crossFade()
.into(messageHolder.mChatMsgImg);
} else {
Log.e("PHOTO_TAG", "No there is no photo");
messageHolder.mChatMsgImg.setVisibility(View.GONE);
}
} catch (Exception e){
//
e.printStackTrace();
}
}
in the try block given above I want to show image if it is a message of type
-Ka81zxkKhzi9tTMy3W3" : {
"messagePhoto" : "https://firebasestorage.googleapis.com/v0/b/MY_APP.appspot.com/o/url_to_image",
"photoUrl" : "http://pbs.twimg.com/profile_images/666473645494112256/pvJyunCa_normal.png",
"title" : "Arshad Ali Soomro",
"uid" : "USER_ID"
}
or show text if it is a message type of
-Ka822evCxQ90EQbfUlQ" : {
"message" : "Hi there",
"photoUrl" : "http://pbs.twimg.com/profile_images/666473645494112256/pvJyunCa_normal.png",
"title" : "Arshad Ali Soomro",
"uid" : "USER_ID"
}
but as you see image is also showing in
-Ka822evCxQ90EQbfUlQ" : {
"message" : "Hi there",
"photoUrl" : "http://pbs.twimg.com/profile_images/666473645494112256/pvJyunCa_normal.png",
"title" : "Arshad Ali Soomro",
"uid" : "USER_ID"
}
how to get rid of that behavior any help please...
Upvotes: 3
Views: 243
Reputation: 544
As i wrote in the comments the solution to the problem is:
Try changing !mMessageList.get(position).getMessagePhoto().equals("") to !mMessageList.get(position).getMessagePhoto()==null. Because if there is no such item in the JSON structure and you are loading the data as an Object from DB it would be null not empty string. Let me know if this helps.
Glad it helped :)
Upvotes: 1