在Android ListView中以错误的顺序异步加载图像

时间:2015-03-18 03:26:38

标签: android android-listview android-asynctask android-imageview

我正在解决问题但我无法解决此问题。我正在访问Facebook API以检索我的应用程序用户,然后我将响应解析为自定义对象。一切正常,直到那里,所以我使用这些信息来填充适配器并将其设置为列表视图。这也很有效,但是当我尝试检索用户Facebook个人资料照片(发出HTTP请求),并在相关的ImageView控件中设置它时,看起来该类丢失了对ImageView的引用并随机设置了listview项目上的照片。

这是否发生在任何人身上?我清楚了吗?

以下是代码:

public class FacebookFriend {
    public String uid;
    public String name;
}


public class ChooseFriends extends Fragment {

    View vw_layout;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (container == null) {
            return null;
        }

        vw_layout = inflater.inflate(R.layout.choosefriend, container, false);

        ListView lst = (ListView) vw_layout.findViewById(R.id.lstFriends);
        new SearchFacebookFriends(getActivity(), lst).execute("");

    return vw_layout;
    }
}

public class FacebookFriendsAdapter extends ArrayAdapter<FacebookFriend> {

    public FacebookFriendsAdapter (Context context, int resource, List<FacebookFriend> objects) {
        super(context, resource, objects);
    }

    private static class ViewHolder {
        ImageView imgFacebookFriend;
        TextView txtFacebookFriendName;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        FacebookFriend friend = getItem(position);

        ViewHolder viewHolder;

        if (convertView == null) {
            viewHolder = new ViewHolder();

            convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_facebook_friend, parent, false);

            viewHolder.imgFacebookFriend = Utils.getImageView(convertView, R.id.imgFacebookFriend);
            viewHolder.txtFacebookFriendName = Utils.getTextView(convertView, R.id.txtFacebookFriendName);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //THIS MEANT TO BE WORKING, FOR EVERY SINGLE ROW, I'M CREATING A NEW INSTANCE PASSING THE CORRECT FACEBOOK UID
        new GetFacebookPhotoAsync(viewHolder.imgFacebookFriend, this).execute(friend.uid);

        //WHEN SET THE NAME WITHOUT AN ASYNC TASK, SHOWS THE CORRECT NAMES
        viewHolder.txtFacebookFriendName.setText(friend.name);

        return convertView;
    }
}

public class SearchFacebookFriends extends AsyncTask<String, Void, String> {

    private WeakReference<Activity> _activity;
    private ProgressDialog progressDialog;
    private ListView listView;

    private Activity activity() {
        return _activity.get();
    }

    public SearchFacebookFriends(Activity activity, ListView listView) {
        this._activity = new WeakReference<Activity>(activity);
        this.listView = listView;
    }

    @Override
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(activity(), "Wait", "...");
    }

    @Override
    protected String doInBackground(String... arg0) {

        //access facebook api and reads app users in json format
        return facebookFriends;
    }

    @Override
    protected void onPostExecute(String result) {

        FacebookFriend[] friends = parseFacebookJsonResponse();

        FacebookFriendsAdapter adapter = new FacebookFriendsAdapter(activity(), 0, Arrays.asList(friends));

        listView.setAdapter(adapter);

        progressDialog.dismiss();
    }

}

public class GetFacebookPhotoAsync extends AsyncTask<String, Void, String> {

    private final WeakReference<ImageView> img;
    private BaseAdapter adapter;

    public BuscaFotoFacebook(ImageView img, BaseAdapter adapter) {
        this.img = new WeakReference<ImageView>(img);
        this.adapter = adapter;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Bitmap doInBackground(String... arg0) {
        //here I make a http request and convert it to a bitmap, works fine
        return Utils.getBitmapFromUrl(String.format("https://graph.facebook.com/%s/picture?width=300&height=300", arg0[0]));
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        //Looks like the reference is lost here
        ImageView _img = img.get();

        if (_img != null) {
            _img.setImageBitmap(result);
            adapter.notifyDataSetChanged();
        }
    }
}

查看正在发生的事情的截图

Repeated photos

Repeated photos

0 个答案:

没有答案