Создал такой адаптер:
public class BookAdapter extends BaseAdapter{
Context ctx;
LayoutInflater lInflater;
Book[] objects;
public BookAdapter(Context context, Book[] books) {
ctx = context;
objects = books;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// кол-во элементов
@Override
public int getCount() {
return objects.length;
}
// элемент по позиции
@Override
public Object getItem(int position) {
return objects[position];
}
// id по позиции
@Override
public long getItemId(int position) {
return position;
}
// пункт списка
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// используем созданные, но не используемые view
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
final Book book = getBook(position);
((TextView) view.findViewById(R.id.tv_author_book)).setText(book.getAuthor());
((TextView) view.findViewById(R.id.tv_name_book)).setText(book.getName() + "");
if (book.isFavorite()) {
((ImageView) view.findViewById(R.id.iv_image_is_favorite_book)).setImageResource(R.drawable.icon_favorite_book_yes);
} else {
((ImageView) view.findViewById(R.id.iv_image_is_favorite_book)).setImageResource(R.drawable.icon_favorite_book_no);
}
((ImageView) view.findViewById(R.id.iv_image_is_favorite_book)).setClickable(true);
((ImageView) view.findViewById(R.id.iv_image_is_favorite_book)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (book.isFavorite()) {
book.setFavorite(false);
} else {
book.setFavorite(true);
}
if (book.isFavorite()) {
((ImageView) view.findViewById(R.id.iv_image_is_favorite_book)).setImageResource(R.drawable.icon_favorite_book_yes);
} else {
((ImageView) view.findViewById(R.id.iv_image_is_favorite_book)).setImageResource(R.drawable.icon_favorite_book_no);
}
}
});
return view;
}
Book getBook(int position) {
return ((Book) getItem(position));
}
}
Мне нужно обновлять listView когда пользователь нажал на ImageView (если book.isFavorite() == false, то убрать из списка). Как это можно сделать?