So I have a list of strings
(ISBNs), and I need to fill a listview
with the objects (Book objects) associated with these strings
. The problem is that the function I have to get the Book object using the string takes time, and so to get, say 30 books, the wait approaches 4 or 5 seconds.
One approach I've thought of is to get the Book objects one at a time, and to add them to the list as I get them. But this process will freeze the UI
until it's done adding them all. If I try to put this process in a new thread, it won't let me add to the any UI
objects (since it's from another thread). If I try to put it in an AsyncTask
, I can't access the ListView
since it's in the MainActivity
class.
There must be a way to dynamically update a UI
element, I'm sure I've seen it done. Any suggestions?
EDIT:
This is the code I'm using to actually add items to the list:
//List view and adapter setup
listView = (ListView) findViewById(R.id.listViewCheckout);
bookAdapter = new SearchBookAdapter(getApplicationContext(), R.layout.search_row_layout);
listView.setAdapter(bookAdapter);
for(int i = 0; i < searches.size(); i++) {
//Get the book
Book book = BackendFunctions.getBookFromISBN(fbSnapshot, searches.get(i));
//Assign data to the adapter variables
Bitmap cover = book.getCover();
String title = book.getTitle();
String author = book.getAuthor();
//Add data to the adapter and set the list
SearchBookDataProvider dataProvider = new SearchBookDataProvider(cover, title, author);
bookAdapter.add(dataProvider);
bookAdapter.notifyDataSetChanged();
}