1

I have a url as

eg: http://movie.org/movie/popular?api_key=c68&page=1

Right now I am able to display for page 1 alone,my question is how to change page and display in listview when scrolling. Right now I am able to display for first page. But I couldn't proceed where to increment page and use it.

My code is as follows:

 int page=1; // i don't know where to add increment of this page and display. 

private class MovieTop extends AsyncTask {

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(
            Object... params) {
        try {
            return displayTopMovies();
        } catch (IOException e) {
            return null;
        }
    }


    public ArrayList<HashMap<String, String>> displayTopMovies()
            throws IOException {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder
                .append("https://movie.org/movie/popular?");
        stringBuilder.append("?api_key=" + "c68"+"&&"+page);
     // right now i am displaying for page 1 alone. I have issue where to add page++ and use it.
        URL url = new URL(stringBuilder.toString());

        InputStream stream = null;
        try {
            // Establish a connection
            HttpURLConnection conn = (HttpURLConnection) url
                    .openConnection();
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.addRequestProperty("Accept", "application/json");              conn.setDoInput(true);
            conn.connect();
            int responseCode = conn.getResponseCode();
            Log.d(DEBUG_TAG, "The response code is: " + responseCode + " "
                    + conn.getResponseMessage());

            stream = conn.getInputStream();
            return parseTopMovies(stringify(stream));
        } finally {
            if (stream != null) {
                stream.close();
            }
        }
    }
      parsing been done here..
    private ArrayList<HashMap<String, String>> parseTopMovies(String result) {
        String streamAsString = result;
        ArrayList<HashMap<String, String>> results = new ArrayList<HashMap<String, String>>();
        try {
            JSONObject jsonObject = new JSONObject(streamAsString);
            JSONArray array = (JSONArray) jsonObject.get("results");
            for (int i = 0; i < array.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject jsonMovieObject = array.getJSONObject(i);
                map.put(KEY_TITLE,
                        jsonMovieObject.getString("original_title"));
                results.add(map);
            }
        } catch (JSONException e) {
            System.err.println(e);
            Log.d(DEBUG_TAG, "Error parsing JSON. String was: "
                    + streamAsString);
        }
        return results;
    }

    public String stringify(InputStream stream) throws IOException,
            UnsupportedEncodingException {
        Reader reader = null;
        reader = new InputStreamReader(stream, "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(reader);
        return bufferedReader.readLine();
    }
}
      @Override
    protected void onPostExecute(Object result) {
        update2((ArrayList<HashMap<String, String>>) result);
    };

 here i am displaying result
  public void update2(ArrayList<HashMap<String, String>> result) {

    ListView listView =(ListView)findViewById(R.id.container);

    // Add results to listView.
    adapter = new UpcomingMovieAdapters(this, R.layout.upcoming,result);
    listView.setAdapter(adapter);
    // Update Activity to show listView
    //setContentView(listView);

}

Thanks in advance.

Shadow
  • 6,864
  • 6
  • 44
  • 93

1 Answers1

0

When the list is scrolled to the end, change the page number to the next number and then fetch the data and update the listview. To detect the end of the listview use http://stackoverflow.com/questions/6358428/implementation-of-onscrolllistener-to-detect-the-end-of-scrolling-in-a-listview. Then change the page number to a higher number and then parse it.. set it to adapter. call notifyDataSetChagned() on adapter :0

Panther
  • 8,938
  • 3
  • 23
  • 34