Android Open Source - android-ribbit-design Message Adapter






From Project

Back to project page android-ribbit-design.

License

The source code is released under:

MIT License

If you think the Android project android-ribbit-design listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.teamtreehouse.ribbit.adapters;
//  w  w  w  .ja  va  2 s.com
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.parse.ParseObject;
import com.teamtreehouse.ribbit.R;
import com.teamtreehouse.ribbit.utils.ParseConstants;

public class MessageAdapter extends ArrayAdapter<ParseObject> {
  
  protected Context mContext;
  protected List<ParseObject> mMessages;
  
  public MessageAdapter(Context context, List<ParseObject> messages) {
    super(context, R.layout.message_item, messages);
    mContext = context;
    mMessages = messages;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    
    if (convertView == null) {
      convertView = LayoutInflater.from(mContext).inflate(R.layout.message_item, null);
      holder = new ViewHolder();
      holder.iconImageView = (ImageView)convertView.findViewById(R.id.messageIcon);
      holder.nameLabel = (TextView)convertView.findViewById(R.id.senderLabel);
      holder.timeLabel = (TextView)convertView.findViewById(R.id.timeLabel);
      convertView.setTag(holder);
    }
    else {
      holder = (ViewHolder)convertView.getTag();
    }
    
    ParseObject message = mMessages.get(position);
    
    Date createdAt = message.getCreatedAt();
    long now = new Date().getTime();
    String convertedDate = DateUtils.getRelativeTimeSpanString(
        createdAt.getTime(), 
        now, 
        DateUtils.SECOND_IN_MILLIS).toString();
    
    holder.timeLabel.setText(convertedDate);
    
    if (message.getString(ParseConstants.KEY_FILE_TYPE).equals(ParseConstants.TYPE_IMAGE)) {
      holder.iconImageView.setImageResource(R.drawable.ic_picture);
    }
    else {
      holder.iconImageView.setImageResource(R.drawable.ic_video);
    }
    holder.nameLabel.setText(message.getString(ParseConstants.KEY_SENDER_NAME));
    
    return convertView;
  }
  
  private static class ViewHolder {
    ImageView iconImageView;
    TextView nameLabel;
    TextView timeLabel;
  }
  
  public void refill(List<ParseObject> messages) {
    mMessages.clear();
    mMessages.addAll(messages);
    notifyDataSetChanged();
  }
}




Java Source Code List

com.teamtreehouse.ribbit.RibbitApplication.java
com.teamtreehouse.ribbit.adapters.MessageAdapter.java
com.teamtreehouse.ribbit.adapters.SectionsPagerAdapter.java
com.teamtreehouse.ribbit.adapters.UserAdapter.java
com.teamtreehouse.ribbit.ui.EditFriendsActivity.java
com.teamtreehouse.ribbit.ui.FriendsFragment.java
com.teamtreehouse.ribbit.ui.InboxFragment.java
com.teamtreehouse.ribbit.ui.LoginActivity.java
com.teamtreehouse.ribbit.ui.MainActivity.java
com.teamtreehouse.ribbit.ui.RecipientsActivity.java
com.teamtreehouse.ribbit.ui.SignUpActivity.java
com.teamtreehouse.ribbit.ui.ViewImageActivity.java
com.teamtreehouse.ribbit.utils.FileHelper.java
com.teamtreehouse.ribbit.utils.ImageResizer.java
com.teamtreehouse.ribbit.utils.MD5Util.java
com.teamtreehouse.ribbit.utils.ParseConstants.java