I have a ListView that lists a set of books.
The user can add a new book, and after that the set of books will be shown again using the onActivityResult method.
I tried for hours to update a set of books after adding a new book, but no luck.
This is the code I tried:
public class BookActivity extends Activity {
private ArrayList<Book> books = null;
private BookItemAdapter bookItemAdapter;
ListView booksSetView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
books = new ArrayList<Book>();
booksSetView = (ListView)findViewById(R.id.booksSet);
Cursor booksCursor = null;
booksCursor = getBooksCursor();
if (booksCursor.moveToFirst())
{
do
{
books.add(getBookFromCursor(booksCursor));
} while(booksCursor.moveToNext());
}
bookItemAdapter = new BookItemAdapter(this, R.layout.book_item, books, 0);
booksSetView.setAdapter(bookItemAdapter);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Log.e("BOOKS MANAGER", "trying to refresh");
bookItemAdapter.notifyDataSetChanged();
booksSetView.invalidate();
booksSetView.invalidateViews();
}
}
Please, help.
Many thanks!
source
share