2017-02-15 95 views
-1

我正在嘗試創建一個聊天頁面(消息視圖)。使用多行佈局實現Android ListView

有圖片和短信。我正在使用Firebase。

所以我嘗試了我自己的代碼。它也顯示圖像。我的意思是不同的佈局工作。但問題是,關閉應用程序後,然後當我重新打開。我看到字符串中的鏈接而不是圖像..

這是我的ArrayAdapter。

public class ChatAdapter extends BaseAdapter { 
ArrayList<String> message = new ArrayList<>(); 
ArrayList<Integer> image = new ArrayList<>(); 

Context c; 
String username; 
String otherusername; 

ArrayList<Integer> type = new ArrayList<>(); 

public ChatAdapter(ArrayList<String> message, ArrayList<Integer> image, Context c, String username, String otherusername, ArrayList<Integer> type) { 
    this.message = message; 
    this.image = image; 
    this.c = c; 
    this.username = username; 
    this.otherusername = otherusername; 
    this.type = type; 
} 


@Override 
public int getCount() { 

    return message.size(); 
} 

@Override 
public Object getItem(int position) { 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

public static class ViewHolder { 
    ImageView imageView; 
    TextView messageView; 
    CircleImageView profileImage; 
} 

@Override 
public int getViewTypeCount() { 
    return 4; 
} 

@Override 
public int getItemViewType(int position) { 
    return type.get (position); 
} 

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) 

@Override 

public View getView(int position, View convertView, ViewGroup parent) { 

    View v = convertView; 
    int messageType = getItemViewType (position); 

    ViewHolder holder; 

    if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     if (messageType==1){ 
      v = View.inflate (c,R.layout.chatroom_image,null); 



     } 
     else { 
      v = View.inflate (c, R.layout.message_item, null); 



     } 




    } 
    if (messageType==1){ 
     holder = new ViewHolder(); 
     holder.imageView = (ImageView) v.findViewById (R.id.imageMessage); 
     holder.profileImage = (CircleImageView) v.findViewById (R.id.profile_image); 
     Picasso.with(c).load(image.get (position)).into(holder.imageView); 


    } 
    else { 
     holder = new ViewHolder(); 
     holder.messageView = (TextView) v.findViewById (R.id.message_view); 
     holder.profileImage = (CircleImageView) v.findViewById (R.id.profile_image); 

     holder.messageView.setText (message.get (position)); 
     holder.profileImage.setImageResource (image.get (position)); 





    } 

    return v; 
} 
} 

在火力地堡數據結構是類似下面..

Firebase Datastructure

 messageList.setAdapter (new ChatAdapter (message_list, profile_img_list, getApplicationContext(), Fusername, chat_username,types)); 

而且我登錄它的完美呈現的一切數據。

感謝您的幫助。

+1

請不要轉貼您已經alread問「的ListView隨着多行佈局」的問題...編輯的問題:明明是你應該做一些更多的研究,因爲我沒有看到'getViewTypeCount'也不'getItemViewType'實施 – Selvin

+0

是的,我問過。並試圖做到這一點。但我遇到了一些問題,@Selvin – Doge

+0

啊我錯過了它。 Thanx @Selvin – Doge

回答

0

當前實現,你會不會有數據填充的項目,如果轉換的觀點是不爲空。這將意味着列表視圖將顯示具有舊數據的項目。

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     // inflate view 

     // populate view 
    } 
} 

嘗試移動代碼以填充空檢查之外的視圖持有者。

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     // inflate view 
    } 

    // always populate view 
}