如何在列表视图中显示朋友的个人资料图片?

时间:2013-08-13 11:40:00

标签: android facebook

我想在列表视图中添加朋友的个人资料图片及其名称。我正在使用代码,在列表视图中给我朋友的名字。但我不知道如何显示他们的个人资料pictures.i已经使用登录按钮,并在其点击事件的朋友列表将打开。任何人都可以告诉。这是我用过的代码

     public class LoginActivity extends Activity{

       public static final String APP_ID = "*************";
       private Facebook facebook = new Facebook(APP_ID);
       private AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
      private ProgressDialog mProgress;
      private Handler mHandler = new Handler();
      private ProgressDialog mSpinner;
      private Handler mRunOnUi = new Handler();
      String FILENAME = "AndroidSSO_data";
      private SharedPreferences mPrefs;

      public static ArrayList<String> friends ;
      String _error;

        public byte[] picture;
        public Bitmap pictureBitmap;
        TextView tv;
         Button loginButton;
        private UiLifecycleHelper uiHelper;
        private ContextWrapper uiActivity;

       @Override
            public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(R.layout.login);
       friends= new ArrayList<String>();

      tv=(TextView)LoginActivity.this.findViewById(R.id.textview1);
                            loginButton=(Button)findViewById(R.id.button_login);

                    loginButton.setOnClickListener(new View.OnClickListener() {

                      @Override
                          public void onClick(View v) {

                        if (! facebook.isSessionValid()) {         
                          facebook.authorize(LoginActivity.this, PERMISSIONS, new LoginDialogListener()); 
                        }
                     }  
              });
            }   

                    public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
        Log.d("FB Demo App", "onActivityResult(): " + requestCode);
                   facebook.authorizeCallback(requestCode, resultCode, data);
                 }

                     private class LoginDialogListener implements DialogListener {

           public void onComplete(Bundle values) {
       saveCredentials(facebook);
     getAlbumsData task = new getAlbumsData();           
          task.execute();
      mHandler.post(new Runnable() {
        public void run() {

                                 mAsyncRunner.request("me/friends", new FriendsRequestListener());

                                     }
                             });
                             }

                       private void saveCredentials(Facebook facebook) {
         }
        public void onFacebookError(FacebookError error) {
                           showToast("Authentication with Facebook failed!");
                       }

                       public void onError(DialogError error) {
                           showToast("Authentication with Facebook failed!");
                       }

                       public void onCancel() {
                           showToast("Authentication with Facebook cancelled!");
                       }
                   }
                public void showToast(String string) {
         }
     public class getAlbumsData {

                            public void execute() {
        }   }
     private class FriendsRequestListener implements RequestListener {
                      String friendData;
       //Method runs when request is complete
                      public void onComplete(String response, Object state) {
                          Log.v("", "FriendListRequestONComplete");

                          friendData = response; 
                          Log.v("friendData--", ""+friendData);
       LoginActivity.this.runOnUiThread(new Runnable() {
         public void run() {
            try {
        //Parse JSON Data
        JSONObject json;
            json = Util.parseJson(friendData);

   JSONArray friendArray = json.getJSONArray("data");
     Log.v("friendArray--", ""+friendArray);
    for(int i = 0; i< friendArray.length(); i++) {
  JSONObject frnd_obj = friendArray.getJSONObject(i);
                                          friends.add(frnd_obj.getString("name"));
                                      }
Intent ide = new Intent(LoginActivity.this,FrndActivity.class);
 ide.putStringArrayListExtra("friends", friends);
  startActivity(ide); 
} 
catch (JSONException e) {
                                      // TODO Auto-generated catch block
                                      e.printStackTrace();
                                  } catch (FacebookError e) {
                                      // TODO Auto-generated catch block
                                      e.printStackTrace();
                                  }
                              }
                          });
                      }

                      }


             }

1 个答案:

答案 0 :(得分:3)

您必须仅使用网址显示朋友的个人资料照片。

您只需使用网址来显示您的frnds个人资料图片。(此工具由Facebook提供)

您可以使用延迟加载 ..

显示您朋友的个人资料照片

<强>代码::::

package com.facebook.me;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.FacebookError;
import com.facebook.lazylist.Friend_List_Lazy_Loading;

public class Friend_list extends Activity
{
    public static final String DATA = "data";
    public static final String NAME = "name";
    public static final String FB_ID = "id";
    public static final String PICTURE = "picture";
    public static final String URL = "url";

    Friend_List_Bean friendBean;
    ArrayList<Friend_List_Bean> aryFriendList;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friend_list);

        aryFriendList = new ArrayList<Friend_List_Bean>();

        getFriendList();
    }

    private void getFriendList() 
    {
        Bundle param = new Bundle();
        param.putString("fields", "name, picture");
        AsyncFacebookRunner async = new AsyncFacebookRunner(MainActivity.facebook);
        async.request("me/friends", param, new RequestListener()
        {

            @Override
            public void onMalformedURLException(MalformedURLException e, Object state) {
            }

            @Override
            public void onIOException(IOException e, Object state) {
            }

            @Override
            public void onFileNotFoundException(FileNotFoundException e, Object state) {
            }

            @Override
            public void onFacebookError(FacebookError e, Object state) {
            }

            @Override
            public void onComplete(String response, Object state) 
            {
                Log.d("response : ", ""+response);
                parseContact(response);
            }
        },null);
    }
    public void parseContact(String response)
    {
        try 
        {
            JSONObject objResponse = new JSONObject(response);

            JSONArray contact = objResponse.getJSONArray(DATA);
            Log.d("Total Friends", " : "+contact.length());
            for(int i=0;i<contact.length();i++)
            {
                friendBean = new Friend_List_Bean();
                JSONObject objData = contact.getJSONObject(i);

                Log.d("", ""+objData.getString(FB_ID));
                Log.d("", ""+objData.getString(NAME));
                Log.d("", ""+objData.getString(PICTURE));

                JSONObject objJsonPicture = objData.getJSONObject(PICTURE);
                JSONObject objJsonPictureData = objJsonPicture.getJSONObject(DATA);
                Log.d("", ""+objJsonPictureData.getString(URL));

                friendBean.setFbId(objData.getString(FB_ID));
                friendBean.setName(objData.getString(NAME));
                friendBean.setPictureUrl(objJsonPictureData.getString(URL));
                aryFriendList.add(friendBean);
            }
            Intent i = new  Intent(Friend_list.this, Friend_List_Lazy_Loading.class);
            i.putExtra("aryFriendList", aryFriendList);
            startActivity(i);
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }

    }
}

<强> BEAN :::::

package com.facebook.me;

import java.io.Serializable;

public class Friend_List_Bean implements Serializable
{
    public String fbId;
    public String name;
    public String pictureUrl;
    public String getFbId() 
    {
        return fbId;
    }
    public void setFbId(String fbId) 
    {
        this.fbId = fbId;
    }
    public String getName() 
    {
        return name;
    }
    public void setName(String name) 
    {
        this.name = name;
    }
    public String getPictureUrl() 
    {
        return pictureUrl;
    }
    public void setPictureUrl(String pictureUrl) 
    {
        this.pictureUrl = pictureUrl;
    }

}

<强> Friend_List_Lazy_Loading.class :::

package com.facebook.lazylist;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

import com.facebook.me.Friend_List_Bean;
import com.facebook.me.Post_on_Friend;
import com.facebook.me.R;

public class Friend_List_Lazy_Loading extends Activity implements OnItemClickListener {

    ListView list;
    GridView gv;
    LazyAdapter adapter;
    GridViewLazyAdapter gridViewLazyAdapter;

   ArrayList<Friend_List_Bean> aryFriendList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

//        aryFriendList = new ArrayList<Friend_List_Bean>();
        list=(ListView)findViewById(R.id.list);

        Intent intent=getIntent();
         aryFriendList=(ArrayList<Friend_List_Bean>)intent.getSerializableExtra("aryFriendList");

        adapter=new LazyAdapter(this, aryFriendList);
        gridViewLazyAdapter=new GridViewLazyAdapter(this, aryFriendList);

        list.setAdapter(adapter);
        list.setOnItemClickListener(this);

        gv = (GridView) findViewById(R.id.grid_id);
        gv.setAdapter(gridViewLazyAdapter);
        gv.setOnItemClickListener(this);        
        Button b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(listener);

        Button btnViewGalary=(Button)findViewById(R.id.btnViewGalary);
        btnViewGalary.setOnClickListener(listenerBtnViewGalary);

        Button btnThumbnail=(Button)findViewById(R.id.btnThumbnail);
        btnThumbnail.setOnClickListener(listenerBtnThumbnail);
        list.setVisibility(View.VISIBLE);
    }

    @Override
    public void onDestroy()
    {
        list.setAdapter(null);
        super.onDestroy();
    }

    public OnClickListener listener=new OnClickListener(){
        @Override
        public void onClick(View arg0) 
        {
            adapter.imageLoader.clearCache();
            adapter.notifyDataSetChanged();
        }
    };

    public OnClickListener listenerBtnViewGalary=new OnClickListener(){
        @Override
        public void onClick(View arg0) 
        {
            list.setVisibility(View.GONE);
            gv.setVisibility(View.VISIBLE);
        }
    };

    public OnClickListener listenerBtnThumbnail=new OnClickListener(){
        @Override
        public void onClick(View arg0) 
        {


            gv.setVisibility(View.GONE);
            list.setVisibility(View.VISIBLE);
        }
    };


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
    {
        if(arg1 == list)
        {
            Toast.makeText(this, aryFriendList.get(arg2).getFbId(), Toast.LENGTH_SHORT).show();
            Intent i = new Intent(Friend_List_Lazy_Loading.this, Post_on_Friend.class);
            i.putExtra("id", ""+aryFriendList.get(arg2).getFbId());
            startActivity(i);
        }
        else
        {
            Toast.makeText(this, ""+aryFriendList.get(arg2).getFbId(), Toast.LENGTH_SHORT).show();
            Intent i = new Intent(Friend_List_Lazy_Loading.this, Post_on_Friend.class);
            i.putExtra("id", aryFriendList.get(arg2).getFbId());
            startActivity(i);
        }

    }
}