Android Open Source - MentorMe Mentor List Adapter






From Project

Back to project page MentorMe.

License

The source code is released under:

MIT License

If you think the Android project MentorMe 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.codepath.wwcmentorme.adapters;
//from   w ww .  ja v  a 2  s. c  o  m
import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.codepath.wwcmentorme.R;
import com.codepath.wwcmentorme.helpers.Constants.Persona;
import com.codepath.wwcmentorme.helpers.Constants.UserDisplayMode;
import com.codepath.wwcmentorme.helpers.Utils;
import com.codepath.wwcmentorme.helpers.ViewHolder;
import com.codepath.wwcmentorme.models.User;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.parse.ParseGeoPoint;

public class MentorListAdapter extends ArrayAdapter<User> {
  private ParseGeoPoint currentGeoPoint;
  private Persona persona;
  private UserDisplayMode userDisplayMode;
  
  private static boolean sImageLoaderInitialized = false;

  public MentorListAdapter(Context context, ParseGeoPoint geoPoint, final Persona persona, final UserDisplayMode mode) {
    super(context, 0);
    currentGeoPoint = geoPoint;
    this.persona = persona;
    this.userDisplayMode = mode;
    if (!sImageLoaderInitialized) {
      sImageLoaderInitialized = true;
      final ImageLoader imageLoader = ImageLoader.getInstance();
      imageLoader.init(ImageLoaderConfiguration.createDefault(getContext()));
    }
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
      LayoutInflater inflator = (LayoutInflater) getContext()
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      view = inflator.inflate(R.layout.mentor_list_item, null);
      final ViewHolder.UserItem holder = new ViewHolder.UserItem();
      holder.ivMentorProfile = (ImageView) view.findViewById(R.id.ivMentorProfile);
      holder.tvFirstName = (TextView) view.findViewById(R.id.tvFirstName);
      holder.tvLastName = (TextView) view.findViewById(R.id.tvLastName);
      holder.tvPosition = (TextView) view.findViewById(R.id.tvPosition);
      holder.tvAbout = (TextView) view.findViewById(R.id.tvAbout);
      holder.tvDistance = (TextView) view.findViewById(R.id.tvDistance);
      holder.tvMenteeCount = (TextView) view.findViewById(R.id.tvMenteeCount);
      holder.llSkills = (LinearLayout) view.findViewById(R.id.llSkills);
      holder.tvSkill1 = (TextView) view.findViewById(R.id.tvSkill1);
      holder.tvSkill2 = (TextView) view.findViewById(R.id.tvSkill2);
      holder.tvSkill3 = (TextView) view.findViewById(R.id.tvSkill3);
      view.setTag(holder);
    }
    final ViewHolder.UserItem holder = (ViewHolder.UserItem) view.getTag();
    final User user = (User) getItem(position);
    try {
      populate(holder, user);
    } catch (JSONException e) {
      e.printStackTrace();
    }    
    return view;
  }
  
  private void populate(final ViewHolder.UserItem holder, User user) throws JSONException {
    final ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.cancelDisplayTask(holder.ivMentorProfile);
    imageLoader.displayImage(user.getProfileImageUrl(200), holder.ivMentorProfile);
    
    holder.tvFirstName.setText(user.getFirstName());
    holder.tvLastName.setText(user.getLastName());
    
    String formattedPosition = user.getJobTitle()  + ", " + user.getCompanyName();
    holder.tvPosition.setText(Html.fromHtml(formattedPosition));
    if (userDisplayMode.equals(UserDisplayMode.CHAT)) {
      holder.tvAbout.setVisibility(View.GONE);
      holder.tvDistance.setVisibility(View.GONE);
      holder.tvMenteeCount.setVisibility(View.GONE);
      holder.llSkills.setVisibility(View.GONE);
    } else {
      if (user.getAboutMe().length() > 120) {
        holder.tvAbout.setText(user.getAboutMe().substring(0, 120) + "...");
      } else {
        holder.tvAbout.setText(user.getAboutMe());
      }
      if(user.getLocation() != null) {
        Double distance = Utils.getDistance(currentGeoPoint.getLatitude(), currentGeoPoint.getLongitude(), user.getLocation().getLatitude(), user.getLocation().getLongitude());
        holder.tvDistance.setText(Utils.formatDouble(distance) + "mi"); 
      }
      final int numMentees = user.getMentees().size();
      if (persona.equals(Persona.MENTEE)) {
        holder.tvMenteeCount.setText("Needs help in");
      } else {
        holder.tvMenteeCount.setText(Utils.formatNumber(Integer
            .toString(numMentees)) + " " + getContext().getResources().getQuantityString(R.plurals.mentee, numMentees));
      }
      holder.tvSkill1.setText("");
      holder.tvSkill2.setText("");
      holder.tvSkill3.setText("");
      final JSONArray skills = persona.equals(Persona.MENTOR) ? user.getMentorSkills() : user.getMenteeSkills();
      if(skills != null && skills.length() > 0) {
        for(int i = 0; i <= skills.length() - 1; i++) {
          if(i == 0) {
            holder.tvSkill3.setText(skills.get(i).toString());
          }
          if(i == 1) {
            holder.tvSkill2.setText(skills.get(i).toString());
          }
          if(i == 2) {
            holder.tvSkill1.setText(skills.get(i).toString());
            break; 
          }
        }
      }
    }
    holder.tvSkill1.setVisibility(holder.tvSkill1.getText().length() == 0 ? View.INVISIBLE : View.VISIBLE);
    holder.tvSkill2.setVisibility(holder.tvSkill2.getText().length() == 0 ? View.INVISIBLE : View.VISIBLE);
    holder.tvSkill3.setVisibility(holder.tvSkill3.getText().length() == 0 ? View.INVISIBLE : View.VISIBLE);
  }
}




Java Source Code List

com.codepath.wwcmentorme.activities.AppActivity.java
com.codepath.wwcmentorme.activities.ChatActivity.java
com.codepath.wwcmentorme.activities.EditProfileActivity.java
com.codepath.wwcmentorme.activities.HomeActivity.java
com.codepath.wwcmentorme.activities.MapActivity.java
com.codepath.wwcmentorme.activities.MentorListActivity.java
com.codepath.wwcmentorme.activities.ThankMentorActivity.java
com.codepath.wwcmentorme.activities.UserListActivity.java
com.codepath.wwcmentorme.activities.ViewProfileActivity.java
com.codepath.wwcmentorme.adapters.ChatAdapter.java
com.codepath.wwcmentorme.adapters.DrawerListAdapter.java
com.codepath.wwcmentorme.adapters.MentorListAdapter.java
com.codepath.wwcmentorme.app.MentorMeApp.java
com.codepath.wwcmentorme.data.DataService.java
com.codepath.wwcmentorme.fragments.AbstractEditProfileFragment.java
com.codepath.wwcmentorme.fragments.EditProfileExperiencesFragment.java
com.codepath.wwcmentorme.fragments.EditProfileLocationFragment.java
com.codepath.wwcmentorme.fragments.EditProfileSkillsFragment.java
com.codepath.wwcmentorme.fragments.RefineResultsDialogFragment.java
com.codepath.wwcmentorme.helpers.Async.java
com.codepath.wwcmentorme.helpers.Constants.java
com.codepath.wwcmentorme.helpers.MentorMeReceiver.java
com.codepath.wwcmentorme.helpers.NotificationCenter.java
com.codepath.wwcmentorme.helpers.RoundedImageView.java
com.codepath.wwcmentorme.helpers.UIUtils.java
com.codepath.wwcmentorme.helpers.Utils.java
com.codepath.wwcmentorme.helpers.ViewHolder.java
com.codepath.wwcmentorme.models.Message.java
com.codepath.wwcmentorme.models.Rating.java
com.codepath.wwcmentorme.models.Request.java
com.codepath.wwcmentorme.models.User.java