我在這個類中創建了一個異步方法,但我找不到執行getAllPaintings方法之前執行它的方法。當我運行它時,這裏是繪畫類我在int size = titles.length;
上得到錯誤所以我該如何執行異步方法。如何從另一個類執行異步方法
package com.ITAN.cinematheque.items;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import com.ITAN.cinematheque.R;
import com.ITAN.cinematheque.activities.MainActivity;
import com.uwetrottmann.tmdb.Tmdb;
import com.uwetrottmann.tmdb.entities.Movie;
import com.uwetrottmann.tmdb.entities.ResultsPage;
import com.uwetrottmann.tmdb.services.MoviesService;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class Painting {
private final String imageUrl;
private final String title;
private final String year;
private final Double location;
static List<Movie> mplist;
static String [] titles;
static String [] rdate;
static String [] imageurl;
static Double [] pop;
static String Image_Base_Url = "http://image.tmdb.org/t/p/w500/";
// String []
private Painting(String imageUrl, String title, String year, Double location) {
this.imageUrl = imageUrl;
this.title = title;
this.year = year;
this.location = location;
//GetXMLTask task = new GetXMLTask();
// Execute the task
//task.execute(new String[] { "http://www.boatshedmarket.com.au/image/data/flowers_img.jpg","http://www.ladybugflorist.com/pictures/home_pic1.jpg","http://www.bitrebels.com/wp-content/uploads/2012/11/electrocuted-flowers-robert-buelteman-7.jpg","http://www.straightbackphysio.co.uk/images/physio-portal/flowers.jpg" });
}
public String getImageUrl() {
return imageUrl;
}
public String getTitle() {
return title;
}
public String getYear() {
return year;
}
public Double getLocation() {
return location;
}
public static Painting[] getAllPaintings(Resources res) {
new showMovieInfo().execute();
// String[] titles = res.getStringArray(R.array.paintings_titles);
// String[] years = res.getStringArray(R.array.paintings_years);
// String[] locations = res.getStringArray(R.array.paintings_locations);
// TypedArray images = res.obtainTypedArray(R.array.paintings_images);
// Bitmap bmp = BitmapFactory.decodeResource(res,R.drawable.sunflowers);
// String[] titles ={"Godzilla","X-Men: Days of Future Past","Neighbors","Divergent","The Fault in Our Stars","The Amazing Spider-Man 2","The Expendables 3","The Prince","Blended","Guardians of the Galaxy","Frozen","Need for Speed","Noah","Maleficent","Sparks","The Lego Movie","Pretty Baby","Sabotage","Street Fighter: Assassin's Fist"
// };
// String[] years ={"Year 1","Year 2","Year 3","Year4"};
// String[] locations={"Location 1","Location 2","Location 3","Location 4"};
// String[] images = {"http://i.imgur.com/DvpvklR.png","http://i.imgur.com/DvpvklR.png","http://i.imgur.com/DvpvklR.png"," http://i.imgur.com/DvpvklR.png"};
// String[] images = {bmp.toString(), bmp.toString(), bmp.toString(), bmp.toString()};
int size = titles.length;
Painting[] paintings = new Painting[size];
for (int i = 0; i < size; i++) {
// paintings[i] = new Painting(images.get(i), titles[i], years[i], locations[i]);
// paintings[i] = new Painting(images.getResourceId(i, -1), titles[i], years[i], locations[i]);
paintings[i] = new Painting(imageurl[i], titles[i], rdate[i], pop[i]);
}
return paintings;
}
public static void executeTask()
{
new showMovieInfo().execute();
}
public static class showMovieInfo extends AsyncTask<Integer,Integer,List<Movie>> {
protected void onPreExecute(Integer f){
//setting up variables for whole method
}
@Override
protected List<Movie> doInBackground(Integer... params) {
Tmdb tmdb = new Tmdb();
tmdb.setApiKey(MYAPIKEY);
MoviesService movieService = tmdb.moviesService();
ResultsPage movie = movieService.popular();//you can use a String[] array at the place of 550.
//String str = String.valueOf(movie.title);
//you can create multiple return statements or u can use if else statement for different commands.
return movie.results;
///MenuInflater inflater = new MenuInflater(MainActivity.this);
//inflater.inflate(android.R.layout.simple_list_item_1, false);
}
protected void onProgressUpdated(Integer... progress)
{
}
protected void onPostExecute(final List<Movie> result)
{
mplist = new List<Movie>() {
@Override
public void add(int location, Movie object) {
}
@Override
public boolean add(Movie object) {
return false;
}
@Override
public boolean addAll(int location, Collection<? extends Movie> collection) {
return false;
}
@Override
public boolean addAll(Collection<? extends Movie> collection) {
return false;
}
@Override
public void clear() {
}
@Override
public boolean contains(Object object) {
return false;
}
@Override
public boolean containsAll(Collection<?> collection) {
return false;
}
@Override
public boolean equals(Object object) {
return false;
}
@Override
public Movie get(int location) {
return null;
}
@Override
public int hashCode() {
return 0;
}
@Override
public int indexOf(Object object) {
return 0;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public Iterator<Movie> iterator() {
return null;
}
@Override
public int lastIndexOf(Object object) {
return 0;
}
@Override
public ListIterator<Movie> listIterator() {
return null;
}
@Override
public ListIterator<Movie> listIterator(int location) {
return null;
}
@Override
public Movie remove(int location) {
return null;
}
@Override
public boolean remove(Object object) {
return false;
}
@Override
public boolean removeAll(Collection<?> collection) {
return false;
}
@Override
public boolean retainAll(Collection<?> collection) {
return false;
}
@Override
public Movie set(int location, Movie object) {
return null;
}
@Override
public int size() {
return 0;
}
@Override
public List<Movie> subList(int start, int end) {
return null;
}
@Override
public Object[] toArray() {
return new Object[0];
}
@Override
public <T> T[] toArray(T[] array) {
return null;
}
};
mplist = result;
String str = String.valueOf(mplist.get(0).release_date);
String str1 = String.valueOf(mplist.get(1).release_date);
String str2 = String.valueOf(mplist.get(2).release_date);
String str3 = String.valueOf(mplist.get(3).release_date);
String str4 = String.valueOf(mplist.get(4).release_date);
String str5 = String.valueOf(mplist.get(5).release_date);
String str6 = String.valueOf(mplist.get(6).release_date);
String str7 = String.valueOf(mplist.get(7).release_date);
String str8 = String.valueOf(mplist.get(8).release_date);
String str9 = String.valueOf(mplist.get(9).release_date);
String str10 = String.valueOf(mplist.get(10).release_date);
String str11 = String.valueOf(mplist.get(11).release_date);
String str12 = String.valueOf(mplist.get(12).release_date);
String str13 = String.valueOf(mplist.get(13).release_date);
String str14 = String.valueOf(mplist.get(14).release_date);
String str15 = String.valueOf(mplist.get(15).release_date);
String str16 = String.valueOf(mplist.get(16).release_date);
String str17 = String.valueOf(mplist.get(17).release_date);
String str18 = String.valueOf(mplist.get(18).release_date);
String str19 = String.valueOf(mplist.get(19).release_date);
titles = new String[]{result.get(0).title,result.get(1).title,result.get(2).title,result.get(3).title,result.get(4).title,result.get(5).title,result.get(6).title,result.get(7).title,result.get(8).title,result.get(9).title,result.get(10).title,result.get(11).title,result.get(12).title,result.get(13).title,result.get(14).title,result.get(15).title,result.get(16).title,result.get(17).title,result.get(18).title,result.get(19).title};
rdate = new String[]{str,str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12,str13,str14,str15,str16,str17,str18,str19};
imageurl = new String[] {Image_Base_Url+result.get(0).poster_path,Image_Base_Url+result.get(1).poster_path,Image_Base_Url+result.get(2).poster_path,Image_Base_Url+result.get(3).poster_path,Image_Base_Url+result.get(4).poster_path,Image_Base_Url+result.get(5).poster_path,Image_Base_Url+result.get(6).poster_path,Image_Base_Url+result.get(7).poster_path,Image_Base_Url+result.get(8).poster_path,Image_Base_Url+result.get(9).poster_path,Image_Base_Url+result.get(10).poster_path,Image_Base_Url+result.get(11).poster_path,Image_Base_Url+result.get(12).poster_path,Image_Base_Url+result.get(13).poster_path,Image_Base_Url+result.get(14).poster_path,Image_Base_Url+result.get(15).poster_path,Image_Base_Url+result.get(16).poster_path,Image_Base_Url+result.get(17).poster_path,Image_Base_Url+result.get(18).poster_path,Image_Base_Url+result.get(19).poster_path};
pop = new Double[]{result.get(0).popularity,result.get(1).popularity,result.get(2).popularity,result.get(3).popularity,result.get(4).popularity,result.get(5).popularity,result.get(6).popularity,result.get(7).popularity,result.get(8).popularity,result.get(9).popularity,result.get(10).popularity,result.get(11).popularity,result.get(12).popularity,result.get(13).popularity,result.get(14).popularity,result.get(15).popularity,result.get(16).popularity,result.get(17).popularity,result.get(18).popularity,result.get(19).popularity};
// tv.setText(str);
//you can use s string array here and use stringarray.add(result) to add multiple items to the array and then you can get those items by using stringarray.get(1).
}
}
}
Display.asyncExec? – StackFlowed 2014-09-05 17:59:52
你能向我解釋爲什麼你重寫List類中的這麼多方法?我真的不知道。 – 2014-09-05 18:24:44