Friends profile picture is not matching with the profile name for facebook in android

╄→гoц情女王★ 提交于 2019-12-13 05:19:07

问题


i am getting friends names,birthdays and profile pictures from Facebook.and I am displaying in listview but profile pictures is not matching with there names. I tried below code:

 public void onComplete(String response, Object state) {

   Log.v("", "FriendListRequestONComplete");
  friendData = response;
 Log.v("friendData--", ""+friendData);

 //Create method to run on UI thread
   MainActivity.this.runOnUiThread(new Runnable() {
   @SuppressLint("NewApi")
   public void run() {

 try {
   //Parse JSON Data
 //  pick(userID);

  JSONObject json;
   //json = Util.parseJson(friendData);
  json = new JSONObject(friendData);


   //Get the JSONArry from our response JSONObject
    friendArray = json.getJSONArray("data");


   Log.v("friendArray--", ""+friendArray);
   for(i = 0; i< friendArray.length(); i++)
   {
      frnd_obj = friendArray.getJSONObject(i);

    try{
                  friends.add("Name:"+frnd_obj.getString("name")+"\n"+"DOB:"+frnd_obj.getString("birthday"));


     String userProfileID=frnd_obj.getString("id");


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

                new DownloadImageTask(img).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "https://graph.facebook.com/"+userProfileID+"/picture?type=small");

            } else{


             new DownloadImageTask(img).execute("https://graph.facebook.com/"+userProfileID+"/picture?type=small");

            }


       }
       catch(Exception e){ 

           //friends.add("Name:"+frnd_obj.getString("name")); 

      }

   }list1.setAdapter(new lsAdapter(MainActivity.this));



  } catch (JSONException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
  } catch (FacebookError e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
 } 
 }
 });

For load profile Pictures Asyntask:

  private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

private ProgressDialog mDialog;
 private ImageView bmImage;
// Bitmap mIcon11 = null;

 public DownloadImageTask(ImageView bmImage) {
     this.bmImage = bmImage;
 }

 protected void onPreExecute() {

      mDialog = ProgressDialog.show(MainActivity.this,"Please wait...", "Retrieving data ...", true);
     mDialog.show();
 }

 protected Bitmap doInBackground(String... urls) {
     Log.d("image", "do in");
     String urldisplay = urls[0];

     try {
         Log.d("image", "do 1");

         InputStream in = new java.net.URL(urldisplay).openStream();
         mIcon11 = BitmapFactory.decodeStream(in);
         Log.d("image", "do 2");
     } catch (Exception e) {
         Log.e("Error", "image download error");
         Log.e("Error", e.getMessage());
       //  mIcon11=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
         e.printStackTrace();
         Log.d("image", "do catch");
     }
     Log.d("image", "do out");
     return mIcon11;
 }

 protected void onPostExecute(Bitmap result) {
     //set image of your imageview
     Log.d("image", "post");
  //   bmImage.setImageResource(R.drawable.ic_launcher);
     bmImage.setImageBitmap(null);
     bmImage.setVisibility(View.INVISIBLE);
     bmImage.setImageBitmap(result);

     if(result!=null){
         //Toast.makeText(getApplicationContext(), "success", 5000).show();
         mIcon11=result;

     }else {
         //Toast.makeText(getApplicationContext(), "Not success", 5000).show();
         mIcon11=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    }
    // bmImage.setImageBitmap(mIcon11);
     bitmapArray.add(mIcon11);

     mDialog.dismiss();

     //close
     //mDialog.dismiss();
 }
 }

This is BaseAdapter class:

  class lsAdapter extends BaseAdapter{
Context context;
public lsAdapter(Context c){
    context=c;

}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    //return friends.size();
    return bitmapArray.size();
}
@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}
@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}
@Override
public View getView(int position, View v, ViewGroup group) {
    // TODO Auto-generated method stub
    LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
    View vi=inflater.inflate(R.layout.customlist, null);
    ImageView iv=(ImageView)vi.findViewById(R.id.iv);
    ImageView next=(ImageView)vi.findViewById(R.id.nextimg);
    TextView tv=(TextView)vi.findViewById(R.id.tv);

        //iv.setImageURI(friendArray.getJSONObject(i).getString("id"));
   tv.setText(friends.get(position));
    iv.setImageBitmap(bitmapArray.get(position));
    return vi;
}
  }

Please help me.to get friends profile picture with there correct names.


回答1:


It seems that you're just adding to the bitmapArray in onPostExecute. But since the ImageDownloadTasks are executed asynchronously, there's no guaranteed order to when onPostExecute will run (an image added later could finish loading earlier). This is probably why you're seeing the random ordering.

Instead of just a bitmapArray, try using a HashMap, with the key being the "id", and the value being the bitmap. Then you can do a lookup in your adapter based on the user id. Alternatively, when you create the ImageDownloadTask, assign it a position, and set the bitmap in the correct position in the array.



来源:https://stackoverflow.com/questions/23466614/friends-profile-picture-is-not-matching-with-the-profile-name-for-facebook-in-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!