朋友个人资料图片与android中的Facebook个人资料名称不匹配

时间:2014-05-05 06:57:26

标签: android facebook-graph-api android-listview android-asynctask android-facebook

我从Facebook收到朋友的名字,生日和个人资料图片。我在列表视图中显示,但个人资料图片与名称不匹配。 我试过下面的代码:

 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:

  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类:

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

请帮助我。用正确的名字获取朋友个人资料照片。

1 个答案:

答案 0 :(得分:0)

您似乎只是在onPostExecute中添加了bitmapArray。但是由于ImageDownloadTasks是异步执行的,所以当onPostExecute运行时(后面添加的图像可以提前完成加载),没有保证顺序。这可能是您看到随机排序的原因。

尝试使用HashMap,而不只是bitmapArray,键是&#34; id&#34;,值是位图。然后,您可以根据用户ID在适配器中进行查找。或者,在创建ImageDownloadTask时,为其指定一个位置,并将位图设置在数组中的正确位置。