Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. public class MessageAdapter extends ArrayAdapter<ChatBubble> {
  2. private Activity activity;
  3. private List<ChatBubble> messages;
  4. private int state;
  5.  
  6. public MessageAdapter(Activity context, int resource, List<ChatBubble> objects) {
  7. super(context, resource, objects);
  8. this.activity = context;
  9. this.messages = objects;
  10. }
  11.  
  12. @Override
  13. public View getView(int position, View convertView, ViewGroup parent) {
  14. ViewHolder holder;
  15. LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
  16.  
  17. int layoutResource = 0; // determined by view type
  18. ChatBubble ChatBubble = getItem(position);
  19. int viewType = getItemViewType(position);
  20.  
  21. switch(ChatBubble.myMessage()){
  22. case "0":
  23. layoutResource = R.layout.date_chat_bubble;
  24. break;
  25. case "1":
  26. layoutResource = R.layout.date_chat_bubble;
  27. break;
  28. case "2":
  29. layoutResource = R.layout.rx_chat_bubble;
  30. break;
  31. }
  32.  
  33. if (convertView != null) {
  34. holder = (ViewHolder) convertView.getTag();
  35. } else {
  36. convertView = inflater.inflate(layoutResource, parent, false);
  37. holder = new ViewHolder(convertView);
  38. convertView.setTag(holder);
  39. }
  40. //set message content
  41. holder.nameRx.setText(ChatBubble.getISSDD());
  42. holder.msg.setText(ChatBubble.getContent());
  43. holder.stringDate.setText( ChatBubble.getstringDate());
  44. holder.stringTime.setText( ChatBubble.getstringTime());
  45. return convertView;
  46. }
  47.  
  48. @Override
  49. public int getViewTypeCount() {
  50. return 3;
  51. }
  52.  
  53. @Override
  54. public int getItemViewType(int position) {
  55. ChatBubble chatBubble = messages.get(position);
  56. switch (chatBubble.myMessage()){
  57. case "0":
  58. state = 1;
  59. break;
  60.  
  61. case "1":
  62. state = 1;
  63. break;
  64.  
  65. case "2":
  66. state = 0;
  67. break;
  68. }
  69. return state;
  70. }
  71.  
  72. private class ViewHolder {
  73.  
  74. private TextView msg;
  75. private TextView stringTime;
  76. private TextView stringDate;
  77. private TextView nameRx;
  78.  
  79. public ViewHolder(View v) {
  80. msg = (TextView) v.findViewById(R.id.txt_msg);
  81. stringTime = (TextView) v.findViewById(R.id.time_msg);
  82. stringDate = (TextView) v.findViewById(R.id.date_msg);
  83. nameRx = (TextView) v.findViewById(R.id.name_rx);
  84. }
  85. }
  86. }
  87.  
  88. public class ChatBubble {
  89.  
  90. private String content;
  91. private String myMessage;
  92. private String stringTime;
  93. private String stringDate;
  94. private String iSsDd;
  95.  
  96. public ChatBubble(String content, String myMessage, String stringTime, String stringDate, String iSsDd) {
  97. this.content = content;
  98. this.myMessage = myMessage;
  99. this.stringTime = stringTime;
  100. this.stringDate = stringDate;
  101. this.iSsDd = iSsDd;
  102. }
  103.  
  104. public String getContent() {
  105. return content;
  106. }
  107.  
  108. public String myMessage() {
  109. return myMessage;
  110. }
  111.  
  112. public String getstringTime() { return stringTime;}
  113.  
  114. public String getstringDate() { return stringDate;}
  115.  
  116. public String getISSDD() { return iSsDd;}
  117. }
  118.  
  119. java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
  120. at com.wsalas.satmessenger.MessageAdapter.getView(MessageAdapter.java:55)
  121. at android.widget.AbsListView.obtainView(AbsListView.java:2828)
  122. at android.widget.ListView.makeAndAddView(ListView.java:1932)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement