0

AsyncTask在我的ListView.

这一切正常,但我不明白为什么我必须在方法中检查我的mHolder.hPositionvs 。如果我不检查这些值,我会得到不正确的旧图像循环- 但是如果它们被调用相同,为什么它们并不总是相等?mPositiononPostExecuteListItemsmHolder.hPositionmPositionAsyncTask

这是我的精简代码:

 @Override
    public View getItemView(final int position, View convertView, final ViewGroup parent) {

        final ViewHolder holder;
        final Job job = (Job) getStore().get(position);

        if (convertView == null) {

            // New List Item

            holder = new ViewHolder();
            convertView = getInflater().inflate(ITEM_LAYOUT, parent, false);

            holder.hPosition = position;
            holder.hImageView = (ImageView) convertView.findViewById(R.id.app_component_joblistitemview_logo);

            convertView.setTag(holder);

        } else {

            // Recycled List Item

            holder = (ViewHolder) convertView.getTag(); // get the ViewHolder that is stored in the Tag
            holder.hPosition = position;
        }

        holder.hImageView.setImageBitmap(null); // set the list item's image to be blank
        new DownloadImageTask().execute(position, holder); // async download the image

        return convertView;
    }


    // DownloadImageTask
    private class DownloadImageTask extends AsyncTask<Object, Void, Integer> {

        Integer mPosition;
        ViewHolder mHolder;

        @Override
        protected Integer doInBackground(Object... params) {

            mPosition = (Integer)params[0];
            mHolder = (ViewHolder)params[1];

            // Download the Image ...

            // ...

            return 1;
        }

        protected void onPostExecute(Integer result) {

            // >> This is the Spot I am Asking About <<
            // Shouldn't mHolder.hPosition ALWAYS be equal to mPosition, since it's still the same AsyncTask?
            // Why are mHolder.hPosition and mPosition often not equal?

            if (mHolder.hPosition==mPosition) {

                // ... update the List View Item

            }
        }

    }
4

1 回答 1

2

下载图像时,您可以继续滚动列表视图。

ViewHolder 可以被回收,并且可以在此时拥有另一个数据。

要检查 ViewHolder 的位置是否正确,您应该检查 holder 上的实际位置是否与在 onPostExecute 上生成下载事件的位置相同。

于 2014-12-12T16:07:13.803 回答