I have done my fair share of research and found three cases related to my problem.
Solution 1
Solution 2
Solution 3
I am not saying that my case is unique or anything,it is jus that i cannot make their answers work for me with my cord.
PROBLEM: I have a listview
getting filtered by an edit text
on top.However when i click on the item searched i get the result of another image being selected.Text works fine but images is the problem.
For example: list has rows with images A,B,C,D
On searching C you get the result filtered as C.But clicking on it results on showing image from A.It is like the position does not change for the images but uses the original ArrayList
adapter position.
Any help will be appreciated.
My adapter:
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:
You can change the code
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();
to
Model model=(Model)adapter.getItem(position);
It will get the model on which position you clicked and pass this model as Serialized or what you want to do.
Hope this will solve problem.
use
Model model = (Model) parent.getItemAtPosition(position);
to retrieve the item from your dataset, and use this reference to fill up the Intent
object. After you filter the items in your Adapter, you are overriding, with modelItems = (List<Model>) results.values;
the current reference of modelItems
.
Edit
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Model model = (Model) parent.getItemAtPosition(position);
Intent intent = new Intent(SearchActivity.this, Details.class);
intent.putExtra(Title, model.getTitle());
intent.putExtra("images", model.getThumbnailUrl());
intent.putExtra(Rate, String.valueOf(model.getRating()));
intent.putExtra(Genre, model.getGenre());
startActivity(intent);
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With