Search code examples
androidandroid-listfragmentbaseadaptercustom-adapter

Filtered custom adapter gets wrong item position


My problem is that filtered custom adapter returns wrong item position. The listfragment is filtered good but when I click on the thumbnail it shows the video corresponding to the unfiltered list.

public static final class PageAdapter extends BaseAdapter implements
        Filterable {
private List<VideoEntry> entries;
private List<VideoEntry> filteredData;
private final List<View> entryViews;
private final Map<YouTubeThumbnailView, YouTubeThumbnailLoader> thumbnailViewToLoaderMap;
private final LayoutInflater inflater;
private final ThumbnailListener thumbnailListener;

private boolean labelsVisible;

public PageAdapter(Context context, List<VideoEntry> entries) {
    this.entries = entries;
    filteredData = entries;

    entryViews = new ArrayList<View>();
    thumbnailViewToLoaderMap = new HashMap<YouTubeThumbnailView, YouTubeThumbnailLoader>();
    inflater = LayoutInflater.from(context);
    thumbnailListener = new ThumbnailListener();
    labelsVisible = true;
}

public void releaseLoaders() {
    for (YouTubeThumbnailLoader loader : thumbnailViewToLoaderMap
            .values()) {
        loader.release();
    }
}

public void setLabelVisibility(boolean visible) {
    labelsVisible = visible;
    for (View view : entryViews) {
        view.findViewById(R.id.text).setVisibility(
                visible ? View.VISIBLE : View.GONE);
    }
}

@Override
public int getCount() {
    return entries.size();
}

@Override
public VideoEntry getItem(int position) {
    return entries.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    try {
        VideoEntry entry = entries.get(position);
            /*
             * int REFRESH_THRESHOLD; if(getCount() - position <=
             * REFRESH_THRESHOLD){ //If there are more items to fetch, and a
             * network request isn't already underway if(loading == false &&
             * has_remaining_items == true){ new JSONParse().execute(); }
             */
            // There are three cases here

        if (view == null) {
        // 1) The view has not yet been created - we need to
        // initialize
        // the YouTubeThumbnailView.
            view = inflater.inflate(R.layout.video_list_item, parent,
                        false);
            YouTubeThumbnailView thumbnail = (YouTubeThumbnailView) view
                    .findViewById(R.id.thumbnail);
            thumbnail.setTag(entry.videoId);
            thumbnail.initialize(DeveloperKey.DEVELOPER_KEY,
                    thumbnailListener);
        } else {
            YouTubeThumbnailView thumbnail = (YouTubeThumbnailView) view
                    .findViewById(R.id.thumbnail);
            YouTubeThumbnailLoader loader = thumbnailViewToLoaderMap
                    .get(thumbnail);
            if (loader == null) {
            // 2) The view is already created, and is currently
            // being
            // initialized. We store the
            // current videoId in the tag.
                thumbnail.setTag(entry.videoId);
            } else {
            // 3) The view is already created and already
            // initialized.
            // Simply set the right videoId
            // on the loader.
            thumbnail.setImageResource(R.drawable.loading_thumbnail);
                    loader.setVideo(entry.videoId);
            }
        }
        TextView label = ((TextView) view.findViewById(R.id.text));
        label.setText(entry.text);
        label.setVisibility(labelsVisible ? View.VISIBLE : View.GONE);
    } catch (IndexOutOfBoundsException e) {
        }
return view;
}

private final class ThumbnailListener implements
    YouTubeThumbnailView.OnInitializedListener,
    YouTubeThumbnailLoader.OnThumbnailLoadedListener {

    @Override
    public void onInitializationSuccess(YouTubeThumbnailView view,
        YouTubeThumbnailLoader loader) {
        loader.setOnThumbnailLoadedListener(this);
        thumbnailViewToLoaderMap.put(view, loader);
        view.setImageResource(R.drawable.loading_thumbnail);
        String videoId = (String) view.getTag();
        loader.setVideo(videoId);
    }

    @Override
    public void onInitializationFailure(YouTubeThumbnailView view,
        YouTubeInitializationResult loader) {
        view.setImageResource(R.drawable.no_thumbnail);
    }

    @Override
    public void onThumbnailLoaded(YouTubeThumbnailView view,
        String videoId) {
    }

    @Override
    public void onThumbnailError(YouTubeThumbnailView view,
        ErrorReason errorReason) {
        view.setImageResource(R.drawable.no_thumbnail);
    }
}

public Filter getFilter() {
    // TODO Auto-generated method stub
    return new Filter() {
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults result) {
                if (result.count == 0)
                    notifyDataSetInvalidated();
                else {
                    entries = (List<VideoEntry>) result.values;
                    notifyDataSetChanged();

            }
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            constraint = constraint.toString().toLowerCase();
            final FilterResults result = new FilterResults();
            if (constraint == null || constraint.length() == 0) {
                result.values = filteredData;
                result.count = filteredData.size();
            } else {

                List<VideoEntry> myList = new ArrayList<VideoEntry>();

                for (int i = 0; i < filteredData.size(); i++) {
                    VideoEntry m = filteredData.get(i);
                    String data = m.text;
                    if (data.toLowerCase().contains(constraint))
                        myList.add(m);
                    }
                result.count = myList.size();
                result.values = myList;

                }

            return result;
        }
    };
}
}

Solution

  • SOLVED! Thank you for your precious help! I pchange two rows onListItemClick : VideoEntry currentEntry = adapter.getItem(position); String videoId = currentEntry.videoId;