So, hey, there was actually a fairly simple solution to last week’s problem that I couldn’t see, as I was unaware of how anonymous inner classes worked in Java. Thank goodness for mentors who nudge you in the right direction!
The code in CategorizationFragment.java that was released in v1.8 of the app:
private void requestSearchResults() {
final CountDownLatch latch = new CountDownLatch(1);
prefixUpdaterSub = new PrefixUpdater(this) {
@Override
protected ArrayList<String> doInBackground(Void... voids) {
ArrayList<String> result = new ArrayList<String>();
try {
result = super.doInBackground();
latch.await();
}
catch (InterruptedException e) {
Log.w(TAG, e);
//Thread.currentThread().interrupt();
}
return result;
}
@Override
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
results.addAll(result);
Log.d(TAG, "Prefix result: " + result);
String filter = categoriesFilter.getText().toString();
ArrayList<String> resultsList = new ArrayList<String>(results);
categoriesCache.put(filter, resultsList);
Log.d(TAG, "Final results List: " + resultsList);
categoriesAdapter.notifyDataSetChanged();
setCatsAfterAsync(resultsList, filter);
}
};
methodAUpdaterSub = new MethodAUpdater(this) {
@Override
protected void onPostExecute(ArrayList<String> result) {
results.clear();
super.onPostExecute(result);
results.addAll(result);
Log.d(TAG, "Method A result: " + result);
categoriesAdapter.notifyDataSetChanged();
latch.countDown();
}
};
Utils.executeAsyncTask(prefixUpdaterSub);
Utils.executeAsyncTask(methodAUpdaterSub);
}
Basically, I could separate most of the preexisting AsyncTask code into the MethodAUpdater.java and PrefixUpdater.java classes (that extend AsyncTask), and then override the methods that I need via anonymous inner classes. This allows me to add the results of the API queries to the ‘results’ member variable of the outer class, in order to aggregate the results in a LinkedHashSet before displaying them to the user.
I’ve released v1.8 to Google Play, here’s to hoping the flexible category suggestions work better for users! 🙂