G
G
Gfd2016-08-10 17:23:51
Java
Gfd, 2016-08-10 17:23:51

How to update custom listView?

Created this adapter:

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

I need to update the listView when the user clicked on the ImageView (if book.isFavorite() == false, then remove it from the list). How can I do that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2016-08-10
@Gfd

1) Throw ListView, use RecyclerView. At least he will force you to write an adapter normally, because what is now is no good.
2) notifyDatasetChanged.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question