Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MessageAdapter extends ArrayAdapter<ChatBubble> {
- private Activity activity;
- private List<ChatBubble> messages;
- private int state;
- public MessageAdapter(Activity context, int resource, List<ChatBubble> objects) {
- super(context, resource, objects);
- this.activity = context;
- this.messages = objects;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder;
- LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
- int layoutResource = 0; // determined by view type
- ChatBubble ChatBubble = getItem(position);
- int viewType = getItemViewType(position);
- switch(ChatBubble.myMessage()){
- case "0":
- layoutResource = R.layout.date_chat_bubble;
- break;
- case "1":
- layoutResource = R.layout.date_chat_bubble;
- break;
- case "2":
- layoutResource = R.layout.rx_chat_bubble;
- break;
- }
- if (convertView != null) {
- holder = (ViewHolder) convertView.getTag();
- } else {
- convertView = inflater.inflate(layoutResource, parent, false);
- holder = new ViewHolder(convertView);
- convertView.setTag(holder);
- }
- //set message content
- holder.nameRx.setText(ChatBubble.getISSDD());
- holder.msg.setText(ChatBubble.getContent());
- holder.stringDate.setText( ChatBubble.getstringDate());
- holder.stringTime.setText( ChatBubble.getstringTime());
- return convertView;
- }
- @Override
- public int getViewTypeCount() {
- return 3;
- }
- @Override
- public int getItemViewType(int position) {
- ChatBubble chatBubble = messages.get(position);
- switch (chatBubble.myMessage()){
- case "0":
- state = 1;
- break;
- case "1":
- state = 1;
- break;
- case "2":
- state = 0;
- break;
- }
- return state;
- }
- private class ViewHolder {
- private TextView msg;
- private TextView stringTime;
- private TextView stringDate;
- private TextView nameRx;
- public ViewHolder(View v) {
- msg = (TextView) v.findViewById(R.id.txt_msg);
- stringTime = (TextView) v.findViewById(R.id.time_msg);
- stringDate = (TextView) v.findViewById(R.id.date_msg);
- nameRx = (TextView) v.findViewById(R.id.name_rx);
- }
- }
- }
- public class ChatBubble {
- private String content;
- private String myMessage;
- private String stringTime;
- private String stringDate;
- private String iSsDd;
- public ChatBubble(String content, String myMessage, String stringTime, String stringDate, String iSsDd) {
- this.content = content;
- this.myMessage = myMessage;
- this.stringTime = stringTime;
- this.stringDate = stringDate;
- this.iSsDd = iSsDd;
- }
- public String getContent() {
- return content;
- }
- public String myMessage() {
- return myMessage;
- }
- public String getstringTime() { return stringTime;}
- public String getstringDate() { return stringDate;}
- public String getISSDD() { return iSsDd;}
- }
- java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
- at com.wsalas.satmessenger.MessageAdapter.getView(MessageAdapter.java:55)
- at android.widget.AbsListView.obtainView(AbsListView.java:2828)
- at android.widget.ListView.makeAndAddView(ListView.java:1932)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement