我需要解析很多页面(大约50个)来获取一些数据的完整列表并将其放入listview中。我只知道如何解析一个页面(通过使用异步任务)并将其放入列表视图。我认为当listview到达页脚时我应该动态地将数据添加到listview中,但是如何? 顺便说一句,我使用Universal Image Loader来显示listview。 请帮忙!
FragmentList类的重要部分(忽略onStart(),它应该在别处)
@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));
}
下载数据类的重要部分
@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;
}
适配器类的重要部分:
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;
}
未正确设置通用图像数据,也忽略它。
答案 0 :(得分:0)
再次调用AsyncTask,您不需要保留引用,因为您无法重用该实例:
new DownloadData(getSherlockActivity()).execute(YOUR_URL);
创建更多的电影对象,并将它们添加到适配器ArrayList中。然后,您可以在适配器上调用notifyDataSetChanged() ...当用户滚动到底部并执行OnScrollListener时,只需执行所有操作。