Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Retrieving and Viewing Messages Creating a Custom List Adapter for Messages

Manfredi Martorana
Manfredi Martorana
19,470 Points

username doesn't show on the list, only icon

Hello everybody, I can't figure out why usernames don't show in the list.

I had tried to give it a sample string, and it worked. The problem is that: message.getString(ParseConstants.KEY_SENDER_NAME) is null, so the label appears blank.

I checked also in parse.com and the sender name is correctly showed.

Here is my code:

public class MessageAdapter extends ArrayAdapter<ParseObject> {

public static final String TAG = MessageAdapter.class.getSimpleName();

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

    if (message.getString(ParseConstants.KEY_FILE_TYPE).equals(ParseConstants.TYPE_IMAGE)) {
        holder.iconImageView.setImageResource(R.drawable.ic_picture);
    }
    else if (message.getString(ParseConstants.KEY_FILE_TYPE).equals(ParseConstants.TYPE_VIDEO)) {
        holder.iconImageView.setImageResource(R.drawable.ic_video);
    }
    else {
        holder.iconImageView.setImageResource(R.drawable.ic_email_white_24dp);
    }

    // BUG TO FIX
    holder.nameLabel.setText(message.getString(ParseConstants.KEY_SENDER_NAME));
    holder.timeLabel.setText(convertedDate);

    return convertView;
}

private static class ViewHolder {
    ImageView iconImageView;
    TextView nameLabel;
    TextView timeLabel;
}

public void refill(List<ParseObject> messages) {
    mMessages.clear();
    mMessages.addAll(messages);
    notifyDataSetChanged();
}

}

Thanks for the help :)