我已经做了充分的研究,找到了三个与我的问题相关的案例。
我并不是说我的情况很特殊或者什么的,只是我无法用他们的答案来解决我的代码问题。
问题:我有一个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:
private List<Model> modelItems
,但在监听器中使用了modelList.get(position)
,为什么会这样?modelList
是什么?它是适配器内部的列表,被更新后,你应该从这里获取你的项目。 - wasyl