Advertisement
skiddhard

TagFriendsAdapter.java

May 31st, 2012
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.82 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. import org.json.JSONArray;
  5. import org.json.JSONObject;
  6.  
  7. import android.app.Activity;
  8. import android.app.ProgressDialog;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.os.Bundle;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.widget.BaseAdapter;
  16. import android.widget.Filter;
  17. import android.widget.Filterable;
  18. import android.widget.FrameLayout;
  19. import android.widget.ImageView;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22.  
  23. import com.dzinesunlimited.sociallyyou.BaseRequestListener;
  24. import com.dzinesunlimited.sociallyyou.R;
  25. import com.dzinesunlimited.sociallyyou.UsersProfile;
  26. import com.dzinesunlimited.sociallyyou.Utility;
  27. import com.dzinesunlimited.sociallyyou.Friends.TagFriends.Friends;
  28. import com.dzinesunlimited.sociallyyou.Utils.ProfilePictureLoader;
  29. import com.facebook.sdk.Facebook;
  30. import com.facebook.sdk.FacebookError;
  31.  
  32. public class TagFriendsAdapter extends BaseAdapter implements Filterable    {
  33.    
  34.     ProgressDialog dialog;
  35.    
  36.     Activity activity;
  37.    
  38.     LayoutInflater inflater = null;
  39.     ProfilePictureLoader imageLoader;
  40.    
  41.     ArrayList<Friends> arrayFriends;
  42.     List<Friends> mOriginalNames;
  43.    
  44.     TagFriendsAdapter(Activity a, ArrayList<Friends> listOfFriends) {
  45.        
  46.         activity = a;
  47.        
  48.         arrayFriends = listOfFriends;
  49.        
  50.         inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  51.         imageLoader = new ProfilePictureLoader(activity.getApplicationContext());
  52.     }
  53.  
  54.     public int getCount() {
  55.         return arrayFriends.size();
  56.     }
  57.  
  58.     public Object getItem(int position) {
  59.         return arrayFriends.get(position);
  60.     }
  61.  
  62.     public long getItemId(int position) {
  63.         return position;
  64.     }
  65.    
  66.     @Override
  67.     public void notifyDataSetChanged() {
  68.         super.notifyDataSetChanged();
  69.     }
  70.    
  71.     public View getView(final int position, View convertView, ViewGroup parent) {
  72.         View vi = convertView;
  73.         if(convertView == null)
  74.             vi = inflater.inflate(R.layout.tag_friends_items, null);
  75.        
  76.         ImageView imgProfilePicture = (ImageView)vi.findViewById(R.id.imgProfilePicture);
  77.         TextView txtUserName = (TextView)vi.findViewById(R.id.txtUserName);
  78.         FrameLayout mainContainer = (FrameLayout) vi.findViewById(R.id.mainContainer);
  79.        
  80.         txtUserName.setText(arrayFriends.get(position).getName());
  81.        
  82.         if (arrayFriends.get(position).getPicture() != null){
  83.             imageLoader.DisplayImage(arrayFriends.get(position).getPicture(), imgProfilePicture);
  84.         } else if (arrayFriends.get(position).getPicture() == null) {
  85.             imgProfilePicture.setVisibility(View.GONE);
  86.         }
  87.        
  88.         mainContainer.setOnClickListener(new View.OnClickListener() {
  89.            
  90.             @Override
  91.             public void onClick(View v) {
  92.                 // TODO Auto-generated method stub
  93. //              Log.e("NAME", arrayFriends.get(position).getName());
  94. //              Log.e("ID", arrayFriends.get(position).getID());
  95.                
  96.                 dialog = ProgressDialog.show(activity, "", "Please wait while we get " +
  97.                         arrayFriends.get(position).getName() + "'s " + "Profile details", true, true);
  98.                 Bundle params = new Bundle();
  99.                 params.putString(Facebook.TOKEN, Utility.mFacebook.getAccessToken());
  100.                 String query = arrayFriends.get(position).getID();
  101.                 Utility.mAsyncRunner.request(query, params, new ProfileListener());
  102.                
  103.             }
  104.         });
  105.        
  106.         return vi;
  107.     }
  108.  
  109.     @Override
  110.     public Filter getFilter() {
  111.        
  112.         Filter filter = new Filter() {
  113.            
  114.             @SuppressWarnings("unchecked")
  115.             @Override
  116.             protected void publishResults(CharSequence constraint, FilterResults results) {
  117.                
  118.                 arrayFriends = (ArrayList<Friends>) results.values;
  119.                 notifyDataSetChanged();
  120.             }
  121.            
  122.             @Override
  123.             protected FilterResults performFiltering(CharSequence constraint) {
  124.                
  125.                 FilterResults results = new FilterResults();
  126.                 ArrayList<Friends> FilteredArrayNames = new ArrayList<Friends>();
  127.                
  128.                 if (mOriginalNames == null) {
  129.                     mOriginalNames = new ArrayList<Friends>(arrayFriends);
  130.                 }
  131.                 if (constraint == null || constraint.length() == 0) {
  132.                     results.count = mOriginalNames.size();
  133.                     results.values = mOriginalNames;
  134.                 } else {
  135.                     constraint = constraint.toString().toLowerCase();
  136.                     for (int i = 0; i < mOriginalNames.size(); i++) {
  137.                         Friends dataNames = mOriginalNames.get(i);
  138.                         if (dataNames.getName().toLowerCase().contains(constraint.toString()))  {
  139.                             FilteredArrayNames.add(dataNames);
  140.                         }
  141.                     }
  142.                    
  143.                     results.count = FilteredArrayNames.size();
  144. //                  System.out.println(results.count);
  145.                    
  146.                     results.values = FilteredArrayNames;
  147. //                  Log.e("VALUES", results.values.toString());
  148.                 }
  149.                
  150.                 return results;
  151.             }
  152.         };
  153.        
  154.         return filter;
  155.     }
  156.    
  157.    
  158.     // PROFILE LISTENER
  159.     public class ProfileListener extends BaseRequestListener {
  160.  
  161.         private String userID;
  162.         private String userName;
  163.         private String userProfilePicture;
  164.         private String userRelationshipStatus;
  165.         private String userSignificantOtherName;
  166.         private String userSignificantOtherID;
  167.         private String userSignificantOtherPic;
  168.         private String userBirthDay;
  169.         private String userGender;
  170.         private String userLanguages;
  171.         private String userLink;
  172.         private String userEmail;
  173.         private String userWebsite;
  174.         private String userBio;
  175.         private StringBuilder langBuilder;
  176.  
  177.         @Override
  178.         public void onComplete(final String response, final Object state) {
  179. //          Log.e("PROFILE DATA", response);
  180.            
  181.             try {
  182.                 JSONObject jsonObject = new JSONObject(response);
  183.                
  184.                 if (jsonObject.has("id"))   {
  185.                     userID = jsonObject.getString("id");
  186. //                  Log.v("ID", userID);
  187.                 } else {
  188.                     userID = null;
  189. //                  Log.v("ID", userID);
  190.                 }
  191.                
  192.                 if (jsonObject.has("name")) {
  193.                     userName = jsonObject.getString("name");
  194. //                  Log.v("NAME", userName);
  195.                 } else {
  196.                     userName = null;
  197. //                  Log.v("NAME", userName);
  198.                 }
  199.                
  200.                 userProfilePicture = "https://graph.facebook.com/"+ userID
  201.                         + "/picture?type=large" + "&access_token=" + Utility.mFacebook.getAccessToken();
  202. //              Log.v("PROFILE PICTURE", userProfilePicture);
  203.                
  204.                 if (jsonObject.has("relationship_status"))  {
  205.                     userRelationshipStatus = jsonObject.getString("relationship_status");
  206. //                  Log.v("RELATIONSHIP STATUS", userRelationshipStatus);
  207.                    
  208.                     if (jsonObject.has("significant_other"))    {
  209.                         JSONObject joSignificantOther = jsonObject.optJSONObject("significant_other");
  210.                         userSignificantOtherName = joSignificantOther.getString("name");
  211.                         userSignificantOtherID = joSignificantOther.getString("id");
  212.                         userSignificantOtherPic = "https://graph.facebook.com/"+ userSignificantOtherID
  213.                                 + "/picture?type=square" + "&access_token=" + Utility.mFacebook.getAccessToken();
  214. //                      Log.v("SIGNIFICANT OTHER NAME", userSignificantOtherName);
  215. //                      Log.v("SIGNIFICANT OTHER ID", userSignificantOtherID);
  216. //                      Log.v("SIGNIFICANT OTHER PICTURE", userSignificantOtherPic);
  217.                     } else {
  218.                         userSignificantOtherName = null;
  219.                         userSignificantOtherID = null;
  220.                         userSignificantOtherPic = null;
  221. //                      Log.v("SIGNIFICANT OTHER NAME", userSignificantOtherName);
  222. //                      Log.v("SIGNIFICANT OTHER ID", userSignificantOtherID);
  223. //                      Log.v("SIGNIFICANT OTHER PICTURE", userSignificantOtherPic);
  224.                     }
  225.                    
  226.                 } else {
  227.                     userRelationshipStatus = null;
  228. //                  Log.v("RELATIONSHIP STATUS", userRelationshipStatus);
  229.                 }
  230.                
  231.                 if (jsonObject.has("birthday")) {
  232.                     userBirthDay = jsonObject.getString("birthday");
  233. //                  Log.v("BIRTHDAY", userBirthDay);
  234.                 } else {
  235.                     userBirthDay = null;
  236. //                  Log.v("BIRTHDAY", userBirthDay);
  237.                 }
  238.                
  239.                 if (jsonObject.has("gender"))   {
  240.                     userGender = jsonObject.getString("gender");
  241. //                  Log.v("GENDER", userGender);
  242.                 } else {
  243.                     userGender = null;
  244. //                  Log.v("GENDER", userGender);
  245.                 }
  246.                
  247.                 if (jsonObject.has("languages"))    {
  248.                     JSONArray jaLang = jsonObject.getJSONArray("languages");
  249.                    
  250.                     langBuilder = new StringBuilder();
  251.                     for(int i = 0; i < jaLang.length(); i ++)   {
  252.                         JSONObject json_data = jaLang.getJSONObject(i);
  253.                        
  254.                         langBuilder.append((json_data.getString("name")) + ("    ") );
  255.                     }
  256.                    
  257.                      userLanguages = langBuilder.toString();
  258. //                   Log.v("LANGUAGES", userLanguages);
  259.                 } else {
  260.                     userLanguages = null;
  261. //                  Log.v("LANGUAGES", userLanguages);
  262.                 }
  263.                
  264.                 if (jsonObject.has("link")) {
  265.                     userLink = jsonObject.getString("link");
  266. //                  Log.v("LINK", userLink);
  267.                 } else {
  268.                     userLink = null;
  269. //                  Log.v("LINK", userLink);
  270.                 }
  271.                
  272.                 if (jsonObject.has("email"))    {
  273.                     userEmail = jsonObject.getString("email");
  274. //                  Log.v("EMAIL", userEmail);
  275.                 } else {
  276.                     userEmail = null;
  277. //                  Log.v("EMAIL", userEmail);
  278.                 }
  279.                
  280.                 if (jsonObject.has("website"))  {
  281.                     userWebsite = jsonObject.getString("website");
  282. //                  Log.v("WEBSITE", userWebsite);
  283. //              } else {
  284.                     userWebsite = null;
  285. //                  Log.v("WEBSITE", userWebsite);
  286.                 }
  287.                
  288.                 if (jsonObject.has("bio"))  {
  289.                     userBio = jsonObject.getString("bio");
  290. //                  Log.v("BIO", userBio);
  291.                 } else {
  292.                     userBio = null;
  293. //                  Log.v("BIO", userBio);
  294.                 }
  295.                
  296.             } catch (Exception e) {
  297.                 e.printStackTrace();
  298.             }
  299.            
  300.             Intent myIntent = new Intent(activity.getApplicationContext(), UsersProfile.class);
  301.            
  302.             myIntent.putExtra("USER_ID", userID);
  303.             myIntent.putExtra("USER_NAME", userName);
  304.             myIntent.putExtra("USER_PROFILE_PICTURE", userProfilePicture);
  305.             myIntent.putExtra("USER_RELATIONSHIP_STATUS", userRelationshipStatus);
  306.             myIntent.putExtra("USER_SIGNIFICANT_OTHER_NAME", userSignificantOtherName);
  307.             myIntent.putExtra("USER_SIGNIFICANT_OTHER_ID", userSignificantOtherID);
  308.             myIntent.putExtra("USER_SIGNIFICANT_OTHER_PROFILE_PICTURE", userSignificantOtherPic);
  309.             myIntent.putExtra("USER_BIRTHDAY", userBirthDay);
  310.             myIntent.putExtra("USER_GENDER", userGender);
  311.             myIntent.putExtra("USER_LANGUAGES", userLanguages);
  312.             myIntent.putExtra("USER_LINK", userLink);
  313.             myIntent.putExtra("USER_EMAIL", userEmail);
  314.             myIntent.putExtra("USER_WEBSITE", userWebsite);
  315.             myIntent.putExtra("USER_BIO", userBio);
  316.            
  317.             dialog.dismiss();
  318.             activity.startActivity(myIntent);
  319.         }
  320.  
  321.         public void onFacebookError(FacebookError error) {
  322.             dialog.dismiss();
  323.             Toast.makeText(activity.getApplicationContext(), "Facebook Error: " + error.getMessage(),
  324.                     Toast.LENGTH_SHORT).show();
  325.         }
  326.     }
  327.  
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement