//This is inside an asynctask
public interface AsyncResponse {
void getKeywords(List<String> s);
void getImageUrls(List<String> s);
}
AsyncResponse obj1=null;
//Inside the asynctask's postExecute method
protected void onPostExecute(List<String> strings) {
super.onPostExecute(strings);
if(url.equals("http://dev.theappsdr.com/apis/photos/keywords.php"))
obj1.getKeywords(strings);
else
obj1.getImageUrls(strings);
}
//First of all, MainActivity implements the interface created in the asynctask
//Call the asynctask like this
APIparsing data = new APIparsing();
data.obj1 = MainActivity.this; //setMainActivity context for the interface object
data.execute("http://dev.theappsdr.com/apis/photos/keywords.php");
//The methods of the implemented interfaces should be defined here.
//These methods run on main thread and can be used to get data from the thread and access UI elements
@Override
public void getKeywords(List < String > s) {
keywordsListSize = s.size();
for (int index = 0; index < s.size(); index++) {
keywordList.add(s.get(index));
}
keywordList.add("Select a Keyword");
adapter = new ArrayAdapter < > (MainActivity.this, android.R.layout.simple_spinner_item, keywordList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Loading.dismiss();
}
@Override
public void getImageUrls(List < String > s) {
if (s.get(0).equals("") && s.size() == 1) {
img.setAlpha(0 f);
Loading.dismiss();
next.setEnabled(false);
prev.setEnabled(false);
Toast.makeText(MainActivity.this, "No Images found for this keyword.", Toast.LENGTH_LONG).show();
} else {
Loading.dismiss();
imageList = s;
Loading.setMessage("Loading Photo...");
Loading.show();
getImageAsync displayImage = new getImageAsync();
displayImage.obj2 = MainActivity.this;
displayImage.execute(imageList.get(i));
}
}