I need to parse a lot of pages (about 50) to get a full list of some data and put it into the listview. I only know how to parse a one page (by using async task) and put it into list view. I think I should dynamically add data into listview when listview reaches its footer, but how? btw, I use Universal Image Loader for displaying listview. Please help!
important part of FragmentList class (ignore onStart(), it should be elsewhere)
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
DownloadData downloadData = new DownloadData(getSherlockActivity());
Log.d("tagg", "kreiran download data");
try {
jsonString = downloadData
.execute(
"http://api.themoviedb.org/3/movie/top_rated?page=3&api_key=deleted")
.get();
jsonObject = new JSONObject(jsonString);
jsonArray = jsonObject.getJSONArray("results");
for (int j = 0; j < jsonArray.length(); j++) {
JSONObject object = jsonArray.getJSONObject(j);
Movie movie = new Movie();
movie.setPoster(object.getString("poster_path"));
movie.setTitle(object.getString("title"));
movie.setScore(object.getDouble("vote_average"));
movie.setId(object.getInt("id"));
movies.add(movie);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setListAdapter(new TopRatedMoviesAdapter(getSherlockActivity(),
R.id.thumbImage, R.id.tvTitle, R.id.tvScore, R.id.tvYear,
R.layout.top_rated_movies_row, movies));
}
Important part of download data class
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String html = "";
try {
HttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(params[0]);
getRequest.setHeader("Accept", "*/*");
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
html = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
}
Log.d("tagg", String.valueOf(statusCode));
} catch (Exception e) {
e.printStackTrace();
Log.d("tagg", "exception u DownloadData");
}
return html;
}
important part of adapter class:
public TopRatedMoviesAdapter(Context context, int posterID, int titleID,
int scoreID, int yearID, int layoutID, ArrayList<Movie> movies) {
super(context, 0);
// TODO Auto-generated constructor stub
this.context = context;
this.movies = movies;
this.titleID = titleID;
this.yearID = yearID;
this.scoreID = scoreID;
this.layoutID = layoutID;
this.posterID = posterID;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(layoutID, null);
poster = (ImageView) row.findViewById(posterID);
title = (TextView) row.findViewById(titleID);
year = (TextView) row.findViewById(yearID);
score = (TextView) row.findViewById(scoreID);
imageLoader.displayImage("http://cf2.imgobject.com/t/p/w45"
+ movies.get(position).getPoster(), poster, options, null);
return null;
}
Universal image data is not properly set, ignore that also.