我正在使用一个线程从我的数据库中获取一个 id。一旦线程完成运行,我需要在另一个线程中使用该 id 来获取项目列表。一旦该线程完成,我将使用项目列表来填充微调器。我遇到的问题是它在线程返回项目列表之前填充微调器。我怎样才能使它在线程完成运行之前不会填充微调器?
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
new Thread(new Runnable() {
@Override
public void run() {
String bookName = existingBooks.get((int) spinner.getSelectedItemId());
Book myBook = AppDatabase.getInstance(getContext()).bookDAO().getBookByTitle(bookName);
long book_Id = myBook.getBook_id();
existingCountDates = AppDatabase.getInstance(getContext()).countDAO().getCountDatesInBook(book_Id);
List<Count> Counts = AppDatabase.getInstance(getContext()).countDAO().getAllCounts();
Log.e("Dates", existingCountDates.toString());
Log.e("All Counts", Counts.toString());
}
}).start();
//TODO: FIX THIS ISSUE
Log.e("Problem:", "This gets called before the thread finishes. Not sure how to fix it");
str_adapter.clear();
for (int i = 0; i < existingCountDates.size(); i++) {
str_adapter.add(existingCountDates.get(i));
}
str_adapter.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
//TODO: DISABLE OTHER DROPDOWN MENUS?
}
});