0

Good Day,

what i am trying to achieve is the following:

Create a infinate scrooling list i found the folowing on GITHUB which i see comes highly recomended by Stackoverflow users.

https://github.com/shontauro/android-pulltorefresh-and-loadmore

the background is as such:

i have a database of posts in my database (over 2000 active posts) i would like to display 20 posts in a listview with the following fields

Pid Name title

then when the user reaches the end of the list it should load further 20 posts.

i know i need to somehow use OnNotifyDataSetChanged(); but have really no idea on how to impliment it.

all the tutorials i have found use a simple array adapter and can only accept a single item :(

The list should also have an onclick listner that will retrieve the post and set it to a new activity.

i can retrieve the posts using JSON from my server but to append the already displayed list has got me stumped.

 public class DownloadJSON extends AsyncTask<Void, Void, Void> {


    @Override
    protected Void doInBackground(Void... params) {
        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions.getJSONfromURL("http://www.example.com/posts.php?last_id=");

        try {
            // Locate the array name in JSON
            jsonarray = jsonobject.getJSONArray("posts");

            for (int i = 0; i < jsonarray.length(); i++) {

                jsonobject = jsonarray.getJSONObject(i);
                // Retrive JSON Objects
                pid = jsonobject.getString("pid");
                name =  jsonobject.getString("name");
                title =  jsonobject.getString("title");


            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

}

if anybody could please point me in the right direction i would really apperieate it

Thanks

Kenny

Kenny
  • 33
  • 8

1 Answers1

0

Because you have the data stored locally in a database it's much better to use CursorAdapter instead of ArrayAdapter. The CursorAdapter will not load 2000 items at the same time making it better in terms of memory usage. It will also manage a lot of work for you related to loading and updating the data.

You'll have to do the following: - create the list with a CursorAdapter for current data - once you reach the end of loaded data make the server call - update the current cursor.

There is a lot of code involved which can't be posted as an answer. The following resources should help get it done:

How to use CursorAdapter: http://www.vogella.com/tutorials/AndroidSQLite/article.html#sqliteoverview_listviews

How to swap the cursor once you have more data loaded in DB: Android SimpleCursorAdapter doesn't update when database changes

Community
  • 1
  • 1
azertiti
  • 3,150
  • 17
  • 19