3

У меня есть несколько сообщений с другой схемой, какКак я могу исправить это странное поведение адаптера recycliewiew?

{ 
    "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" 
    } 
    } 
} 

У меня есть это странное поведение RecyclerView адаптера выглядеть

enter image description here

пользовательского RecyclerView, который может показать текст или изображение в соответствии с логикой моего макета XML это

<?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> 
адаптер

onBindViewHolder() является

@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(); 
    } 


} 

в блоке Ьгу приведенный выше, я хочу показать изображение, если это сообщение типа

-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" 
    } 

но как вы видите изображение также показано в

-Ka822evCxQ90EQbfUlQ" : { 
     "message" : "Hi there", 
     "photoUrl" : "http://pbs.twimg.com/profile_images/666473645494112256/pvJyunCa_normal.png", 
     "title" : "Arshad Ali Soomro", 
     "uid" : "USER_ID" 
    } 

как сделать избавиться от этого поведения любой помощи, пожалуйста ...

+0

O Человек! почему проголосовать. , ? –

+2

вы положите хорошее усилие в вопросе вопроса и сделаете видео +1 для этого – Redman

+0

@Redman Большое спасибо, но любое предложение/решение этого, пожалуйста, это будет спасательный ... –

ответ

1

Как я писал в комментариях решения проблемы является:

Попробуйте изменить mMessageList.get (положение) .getMessagePhoto() равно (!. "") to! MMessageList.get (position) .getMessagePhoto() == null. Потому что если нет такого элемента в структуре JSON, и вы загружаете данные в качестве объекта из БД, это будет пустая строка. Позвольте мне знать, если это поможет.

Рад, что помог :)