朋友的个人资料图片不与配置文件名匹配的android系统中的Facebook [英] Friends profile picture is not matching with the profile name for facebook in android

查看:260
本文介绍了朋友的个人资料图片不与配置文件名匹配的android系统中的Facebook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到朋友的名字,生日和资料照片从Facebook.and我在ListView中显示,但个人资料图片不与有名称匹配。
我想下面code:

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();
 } 
 }
 });

有关负载配置图片Asyntask:

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();
 }
 }

这是BaseAdapter类:

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;
}
  }

请帮忙me.to得到朋友的个人资料图片与有正确的名称。

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

推荐答案

看来你只是增加了bitmapArray在onPostExecute。但由于ImageDownloadTasks是异步执行的,没有保证的顺序时onPostExecute运行(后来又增加可能会提前完成加载图像)。这可能是为什么你看到的随机排序。

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.

而不是仅仅一个bitmapArray,请尝试使用一个HashMap,键为标识,该值是位图。然后,你可以做基于用户ID在适配器中查找。另外,当您创建ImageDownloadTask,分配给它的位置,并设置位图数组中的正确位置。

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.

这篇关于朋友的个人资料图片不与配置文件名匹配的android系统中的Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆