Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.Filter;
- import android.widget.Filterable;
- import android.widget.FrameLayout;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.dzinesunlimited.sociallyyou.BaseRequestListener;
- import com.dzinesunlimited.sociallyyou.R;
- import com.dzinesunlimited.sociallyyou.UsersProfile;
- import com.dzinesunlimited.sociallyyou.Utility;
- import com.dzinesunlimited.sociallyyou.Friends.TagFriends.Friends;
- import com.dzinesunlimited.sociallyyou.Utils.ProfilePictureLoader;
- import com.facebook.sdk.Facebook;
- import com.facebook.sdk.FacebookError;
- public class TagFriendsAdapter extends BaseAdapter implements Filterable {
- ProgressDialog dialog;
- Activity activity;
- LayoutInflater inflater = null;
- ProfilePictureLoader imageLoader;
- ArrayList<Friends> arrayFriends;
- List<Friends> mOriginalNames;
- TagFriendsAdapter(Activity a, ArrayList<Friends> listOfFriends) {
- activity = a;
- arrayFriends = listOfFriends;
- inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- imageLoader = new ProfilePictureLoader(activity.getApplicationContext());
- }
- public int getCount() {
- return arrayFriends.size();
- }
- public Object getItem(int position) {
- return arrayFriends.get(position);
- }
- public long getItemId(int position) {
- return position;
- }
- @Override
- public void notifyDataSetChanged() {
- super.notifyDataSetChanged();
- }
- public View getView(final int position, View convertView, ViewGroup parent) {
- View vi = convertView;
- if(convertView == null)
- vi = inflater.inflate(R.layout.tag_friends_items, null);
- ImageView imgProfilePicture = (ImageView)vi.findViewById(R.id.imgProfilePicture);
- TextView txtUserName = (TextView)vi.findViewById(R.id.txtUserName);
- FrameLayout mainContainer = (FrameLayout) vi.findViewById(R.id.mainContainer);
- txtUserName.setText(arrayFriends.get(position).getName());
- if (arrayFriends.get(position).getPicture() != null){
- imageLoader.DisplayImage(arrayFriends.get(position).getPicture(), imgProfilePicture);
- } else if (arrayFriends.get(position).getPicture() == null) {
- imgProfilePicture.setVisibility(View.GONE);
- }
- mainContainer.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- // Log.e("NAME", arrayFriends.get(position).getName());
- // Log.e("ID", arrayFriends.get(position).getID());
- dialog = ProgressDialog.show(activity, "", "Please wait while we get " +
- arrayFriends.get(position).getName() + "'s " + "Profile details", true, true);
- Bundle params = new Bundle();
- params.putString(Facebook.TOKEN, Utility.mFacebook.getAccessToken());
- String query = arrayFriends.get(position).getID();
- Utility.mAsyncRunner.request(query, params, new ProfileListener());
- }
- });
- return vi;
- }
- @Override
- public Filter getFilter() {
- Filter filter = new Filter() {
- @SuppressWarnings("unchecked")
- @Override
- protected void publishResults(CharSequence constraint, FilterResults results) {
- arrayFriends = (ArrayList<Friends>) results.values;
- notifyDataSetChanged();
- }
- @Override
- protected FilterResults performFiltering(CharSequence constraint) {
- FilterResults results = new FilterResults();
- ArrayList<Friends> FilteredArrayNames = new ArrayList<Friends>();
- if (mOriginalNames == null) {
- mOriginalNames = new ArrayList<Friends>(arrayFriends);
- }
- if (constraint == null || constraint.length() == 0) {
- results.count = mOriginalNames.size();
- results.values = mOriginalNames;
- } else {
- constraint = constraint.toString().toLowerCase();
- for (int i = 0; i < mOriginalNames.size(); i++) {
- Friends dataNames = mOriginalNames.get(i);
- if (dataNames.getName().toLowerCase().contains(constraint.toString())) {
- FilteredArrayNames.add(dataNames);
- }
- }
- results.count = FilteredArrayNames.size();
- // System.out.println(results.count);
- results.values = FilteredArrayNames;
- // Log.e("VALUES", results.values.toString());
- }
- return results;
- }
- };
- return filter;
- }
- // PROFILE LISTENER
- public class ProfileListener extends BaseRequestListener {
- private String userID;
- private String userName;
- private String userProfilePicture;
- private String userRelationshipStatus;
- private String userSignificantOtherName;
- private String userSignificantOtherID;
- private String userSignificantOtherPic;
- private String userBirthDay;
- private String userGender;
- private String userLanguages;
- private String userLink;
- private String userEmail;
- private String userWebsite;
- private String userBio;
- private StringBuilder langBuilder;
- @Override
- public void onComplete(final String response, final Object state) {
- // Log.e("PROFILE DATA", response);
- try {
- JSONObject jsonObject = new JSONObject(response);
- if (jsonObject.has("id")) {
- userID = jsonObject.getString("id");
- // Log.v("ID", userID);
- } else {
- userID = null;
- // Log.v("ID", userID);
- }
- if (jsonObject.has("name")) {
- userName = jsonObject.getString("name");
- // Log.v("NAME", userName);
- } else {
- userName = null;
- // Log.v("NAME", userName);
- }
- userProfilePicture = "https://graph.facebook.com/"+ userID
- + "/picture?type=large" + "&access_token=" + Utility.mFacebook.getAccessToken();
- // Log.v("PROFILE PICTURE", userProfilePicture);
- if (jsonObject.has("relationship_status")) {
- userRelationshipStatus = jsonObject.getString("relationship_status");
- // Log.v("RELATIONSHIP STATUS", userRelationshipStatus);
- if (jsonObject.has("significant_other")) {
- JSONObject joSignificantOther = jsonObject.optJSONObject("significant_other");
- userSignificantOtherName = joSignificantOther.getString("name");
- userSignificantOtherID = joSignificantOther.getString("id");
- userSignificantOtherPic = "https://graph.facebook.com/"+ userSignificantOtherID
- + "/picture?type=square" + "&access_token=" + Utility.mFacebook.getAccessToken();
- // Log.v("SIGNIFICANT OTHER NAME", userSignificantOtherName);
- // Log.v("SIGNIFICANT OTHER ID", userSignificantOtherID);
- // Log.v("SIGNIFICANT OTHER PICTURE", userSignificantOtherPic);
- } else {
- userSignificantOtherName = null;
- userSignificantOtherID = null;
- userSignificantOtherPic = null;
- // Log.v("SIGNIFICANT OTHER NAME", userSignificantOtherName);
- // Log.v("SIGNIFICANT OTHER ID", userSignificantOtherID);
- // Log.v("SIGNIFICANT OTHER PICTURE", userSignificantOtherPic);
- }
- } else {
- userRelationshipStatus = null;
- // Log.v("RELATIONSHIP STATUS", userRelationshipStatus);
- }
- if (jsonObject.has("birthday")) {
- userBirthDay = jsonObject.getString("birthday");
- // Log.v("BIRTHDAY", userBirthDay);
- } else {
- userBirthDay = null;
- // Log.v("BIRTHDAY", userBirthDay);
- }
- if (jsonObject.has("gender")) {
- userGender = jsonObject.getString("gender");
- // Log.v("GENDER", userGender);
- } else {
- userGender = null;
- // Log.v("GENDER", userGender);
- }
- if (jsonObject.has("languages")) {
- JSONArray jaLang = jsonObject.getJSONArray("languages");
- langBuilder = new StringBuilder();
- for(int i = 0; i < jaLang.length(); i ++) {
- JSONObject json_data = jaLang.getJSONObject(i);
- langBuilder.append((json_data.getString("name")) + (" ") );
- }
- userLanguages = langBuilder.toString();
- // Log.v("LANGUAGES", userLanguages);
- } else {
- userLanguages = null;
- // Log.v("LANGUAGES", userLanguages);
- }
- if (jsonObject.has("link")) {
- userLink = jsonObject.getString("link");
- // Log.v("LINK", userLink);
- } else {
- userLink = null;
- // Log.v("LINK", userLink);
- }
- if (jsonObject.has("email")) {
- userEmail = jsonObject.getString("email");
- // Log.v("EMAIL", userEmail);
- } else {
- userEmail = null;
- // Log.v("EMAIL", userEmail);
- }
- if (jsonObject.has("website")) {
- userWebsite = jsonObject.getString("website");
- // Log.v("WEBSITE", userWebsite);
- // } else {
- userWebsite = null;
- // Log.v("WEBSITE", userWebsite);
- }
- if (jsonObject.has("bio")) {
- userBio = jsonObject.getString("bio");
- // Log.v("BIO", userBio);
- } else {
- userBio = null;
- // Log.v("BIO", userBio);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- Intent myIntent = new Intent(activity.getApplicationContext(), UsersProfile.class);
- myIntent.putExtra("USER_ID", userID);
- myIntent.putExtra("USER_NAME", userName);
- myIntent.putExtra("USER_PROFILE_PICTURE", userProfilePicture);
- myIntent.putExtra("USER_RELATIONSHIP_STATUS", userRelationshipStatus);
- myIntent.putExtra("USER_SIGNIFICANT_OTHER_NAME", userSignificantOtherName);
- myIntent.putExtra("USER_SIGNIFICANT_OTHER_ID", userSignificantOtherID);
- myIntent.putExtra("USER_SIGNIFICANT_OTHER_PROFILE_PICTURE", userSignificantOtherPic);
- myIntent.putExtra("USER_BIRTHDAY", userBirthDay);
- myIntent.putExtra("USER_GENDER", userGender);
- myIntent.putExtra("USER_LANGUAGES", userLanguages);
- myIntent.putExtra("USER_LINK", userLink);
- myIntent.putExtra("USER_EMAIL", userEmail);
- myIntent.putExtra("USER_WEBSITE", userWebsite);
- myIntent.putExtra("USER_BIO", userBio);
- dialog.dismiss();
- activity.startActivity(myIntent);
- }
- public void onFacebookError(FacebookError error) {
- dialog.dismiss();
- Toast.makeText(activity.getApplicationContext(), "Facebook Error: " + error.getMessage(),
- Toast.LENGTH_SHORT).show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement