gpt4 book ai didi

android - ListView 上的搜索过滤器在项目单击时返回错误结果

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:15:53 26 4
gpt4 key购买 nike

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

Solution 1

Solution 2

Solution 3

我并不是说我的案例是独一无二的或其他什么,只是我无法用我的电源线让他们的答案对我有用。

问题: 我有一个 listview 被顶部的 edit 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:

最佳答案

您可以更改代码

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

它将获取您点击位置的模型,并将该模型作为序列化或您想要执行的操作传递。

希望这能解决问题。

关于android - ListView 上的搜索过滤器在项目单击时返回错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32849989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com