0

我需要解析很多页面(大约 50 个)以获取一些数据的完整列表并将其放入列表视图中。我只知道如何解析一页(通过使用异步任务)并将其放入列表视图。我认为当列表视图到达其页脚时,我应该动态地将数据添加到列表视图中,但是如何?顺便说一句,我使用通用图像加载器来显示列表视图。请帮忙!

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;
}

通用图像数据未正确设置,也请忽略。

4

1 回答 1

0

再次调用 AsyncTask,不需要保留引用,因为不能重用实例:

new DownloadData(getSherlockActivity()).execute(YOUR_URL);

创建更多电影对象,然后像以前一样将它们添加到适配器 ArrayList 中。然后你可以在你的适配器上调用notifyDataSetChanged() ......当用户滚动到实现OnScrollListener的底部时,只需执行所有这些操作。

于 2013-07-25T21:35:05.260 回答