列表视图上的搜索过滤器会在项目点击

时间:2015-09-29 17:14:58

标签: android android-filterable

我做了相当多的研究,发现了三个与我的问题相关的案例。

Solution 1

Solution 2

Solution 3

我并不是说我的案件是独一无二的,这是不可能的,我无法用我的绳索让他们的答案适合我。

问题:我的listviewedit text过滤了。但是当我点击搜索的项目时,我会得到另一张图片的结果。文本工作正常,但图像是问题。

例如:列表包含图片A,B,C,D

的行

在搜索C时,您将结果过滤为C.但是单击它会导致显示来自A的图像。它就像图像的位置没有变化但是使用原始的ArrayList适配器位置。

任何帮助将不胜感激。

我的适配器:

public class SearchListAdapter extends BaseAdapter implements Filterable {
    List<Model> mStringFilterList;
    ValueFilter valueFilter;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    private Activity activity;
    private LayoutInflater inflater;
    private List<Model> modelItems;
    private int mDefaultImageId;

    public SearchListAdapter(Activity activity, List<Model> modelItems) {
        this.activity = activity;
        this.modelItems = modelItems;
        mStringFilterList = modelItems;
    }


    public void setDefaultImageResId(int defaultImage) {
        mDefaultImageId = defaultImage;
    }

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

    @Override
    public Object getItem(int location) {
        return modelItems.get(location);
    }

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

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

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.search_list_row, null);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
        NetworkImageView thumbNail = (NetworkImageView) convertView
                .findViewById(R.id.thumbnail);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        TextView rating = (TextView) convertView.findViewById(R.id.rating);
        TextView genre = (TextView) convertView.findViewById(R.id.genre);

        Model m = modelItems.get(position);

        // thumbnail image
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
        thumbNail.setTag(position);
        // title
        title.setText(m.getTitle());

        // amount
        rating.setText(String.valueOf(m.getRating()));

        // description
        String genreStr = "";
        for (String str : m.getGenre()) {
            genreStr += str + ", ";
        }
        genreStr = genreStr.length() > 0 ? genreStr.substring(0,
                genreStr.length() - 2) : genreStr;
        genre.setText(genreStr);

        return convertView;
    }

    @Override
    public Filter getFilter() {
        if (valueFilter == null) {
            valueFilter = new ValueFilter();
        }
        return valueFilter;
    }

    private class ValueFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint != null && constraint.length() > 0) {
                ArrayList<Model> filterList = new ArrayList<Model>();
                for (int i = 0; i < mStringFilterList.size(); i++)
                    if ((mStringFilterList.get(i).getTitle().toUpperCase())
                            .contains(constraint.toString().toUpperCase())) {


                        Model m = new Model(mStringFilterList.get(i)
                                .getTitle(), mStringFilterList.get(i)
                                .getThumbnailUrl(), mStringFilterList.get(i)
                                .getRating(), mStringFilterList.get(i)
                                .getGenre());

                        filterList.add(m);
                    }
                results.count = filterList.size();
                results.values = filterList;
            } else {
                results.count = mStringFilterList.size();
                results.values = mStringFilterList;
            }
            return results;

        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            modelItems = (List<Model>) results.values;
            notifyDataSetChanged();
        }

    }

}

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String name = ((TextView) view.findViewById(R.id.title))
                        .getText().toString();
                bitmap = ((Model) modelList.get(position)).getThumbnailUrl();
                String rate = ((TextView) view.findViewById(R.id.rating))
                        .getText().toString();
                String genres = ((TextView) view.findViewById(R.id.genre))
                        .getText().toString();

                Intent intent = new Intent(SearchActivity.this, Details.class);
                intent.putExtra(Title, name);
                intent.putExtra("images", bitmap);
                intent.putExtra(Rate, rate);
                intent.putExtra(Genre, genres);

                startActivity(intent);
            }
        });
    }

OnItemclickListener:

4 个答案:

答案 0 :(得分:6)

使用

class Article(models.Model):
   .....
class ArticleTag(models.Model):
   tag = models.CharField(max_length = 25)
   article = models.ForeignKey(Article)

class Comic(models.Model):
    .....
class ComicTag(models.Model):
    tag = models.CharField(max_length = 25)
    comic = models.ForeignKey(Comic)

从数据集中检索项目,并使用此引用填充 Model model = (Model) parent.getItemAtPosition(position); 对象。在您过滤适配器中的项目后,您将覆盖Intent当前引用modelItems = (List<Model>) results.values;

修改

modelItems

答案 1 :(得分:6)

您可以更改代码

String name = ((TextView) view.findViewById(R.id.title)).getText().toString();
bitmap = ((Model) modelList.get(position)).getThumbnailUrl();
String rate = ((TextView) view.findViewById(R.id.rating)).getText().toString();
String genres = ((TextView) view.findViewById(R.id.genre)).getText().toString();

Model model=(Model)adapter.getItem(position);

它将获取您点击的位置的模型,并将此模型作为序列化或您想要做的。

希望这能解决问题。

答案 2 :(得分:2)

您似乎正在过滤适配器,但随后从原始未过滤列表中获取项目。您应该直接从适配器获取该项目。将以下方法添加到BaseAdapter:

public Model getItemAtPosition(int position)
{
    return model.get(position);
}

并从OnItemClickListener实施中调用

bitmap = adapter.getItemAtPosition(position).getThumbnailUrl();

答案 3 :(得分:1)

问题是onClickListenerposition=0时返回onClick,因为在您已过滤的ListView中,它只有一个项目C index=0第零索引是指image A,这就是为什么显示A image 请参阅此解决方案:Get Original Index from Filtered list view