I have few functions where I am having a conflict. I am using AsyncTask class in Android, to make calls to Tmdb API, so that I can retrieve images and text already stored in JSON Data.
Here is the code:
public void prepareImages (SimpleAdapter adapter){
ListView listView = new ListView(this);
listView.setAdapter(adapter);
for(int i=0;i<adapter.getCount();i++){
HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
String imgUrl = (String) hm.get("flag_path");
Display_With_Image imageLoaderTask = new Display_With_Image();
HashMap<String, Object> hmDownload = new HashMap<String, Object>();
hm.put("poster_path",imgUrl);
hm.put("position", i);
// Starting ImageLoaderTask to download and populate image in the listview
imageLoaderTask.execute(hm);
}
}
Got error here at last line - call for execute function: - The method execute(HashMap) is undefined for the type Display_With_Image
The other class is:
class ImageDownloading extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{
@Override
protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {
InputStream iStream=null;
String imgUrl = (String) hm[0].get("poster_path");
int position = (Integer) hm[0].get("position");
... more code ...
This method doInBackground is called when execute is actually called, I know this, but I can't solve the error, since the parameter defined is a hashmap having String, Object and so is my object "hm" in the prepareImages() function.
Please let me know what's wrong, i only see a difference that I am passing parameter of type HashMap, but in the parameter defined it shows Hashmap with two extra dots ".."
EDIT: Display_With_Image
public class Display_With_Image extends Activity {
public void displayDownloadedImages (HashMap<String, Object> result){
// Getting the path to the downloaded image
ListView listView = new ListView (this);
String path = (String) result.get("poster_path");
// Getting the position of the downloaded image
int position = (Integer) result.get("position");
// Getting adapter of the listview
SimpleAdapter adapter = (SimpleAdapter ) listView.getAdapter();
// Getting the hashmap object at the specified position of the listview
HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(position);
// Overwriting the existing path in the adapter
hm.put("position_path",path);
// Noticing listview about the dataset changes
adapter.notifyDataSetChanged();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imdbsearch_result);
//getActionBar().setDisplayHomeAsUpEnabled(true);
// Get the intent to get the query.
Intent intent = getIntent();
String query = intent.getStringExtra(MainActivity.EXTRA_QUERY);
}
class ImageDownloading extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{
@Override
protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {
InputStream iStream=null;
String imgUrl = (String) hm[0].get("poster_path");
int position = (Integer) hm[0].get("position");
URL url;
try {
url = new URL(imgUrl);
// Creating an http connection to communicate with url
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
// Getting Caching directory
File cacheDirectory = getBaseContext().getCacheDir();
// Temporary file to store the downloaded image
File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+position+".png");
// The FileOutputStream to the temporary file
FileOutputStream fOutStream = new FileOutputStream(tmpFile);
// Creating a bitmap from the downloaded inputstream
Bitmap b = BitmapFactory.decodeStream(iStream);
// Writing the bitmap to the temporary file as png file
b.compress(Bitmap.CompressFormat.PNG,100, fOutStream);
// Flush the FileOutputStream
fOutStream.flush();
//Close the FileOutputStream
fOutStream.close();
// Create a hashmap object to store image path and its position in the listview
HashMap<String, Object> hmBitmap = new HashMap<String, Object>();
// Storing the path to the temporary image file
hmBitmap.put("flag",tmpFile.getPath());
// Storing the position of the image in the listview
hmBitmap.put("position",position);
// Returning the HashMap object containing the image path and position
return hmBitmap;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(HashMap<String, Object> result) {
displayDownloadedImages(result);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
}
Your imageLoaderTask
is an object of Display_With_Image
class which extends Activity
.
Activity
does not have an execute
method. AsyncTask
does.
You want to create an object of ImageDownloading
Change
Display_With_Image imageLoaderTask = new Display_With_Image();
to
ImageDownloading imageLoaderTask = new ImageDownloading();
then you will be able to call execute
on imageLoaderTask