0

In my list I wanna Implement endless list. for now when I scroll down and receive to fifth item of list, new data load but data not add to end of list, it clear all item of list and then show.

How append new item to end of list. Notice that I'm using adapter.notifyDataSetChanged(); but still don't work for me.

here my EndlessScrollListener class:

public class EndlessScrollListener implements AbsListView.OnScrollListener {

        private int visibleThreshold = 5;
       // private int currentPage = 0;

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (scrollState == SCROLL_STATE_IDLE) {
                if (myList.getLastVisiblePosition() >= myList.getCount() - visibleThreshold) {
                    cpage++;
                    new sendpage(cpage).execute();
                    if (isOnline()) {
                        requestData("http://192.168.1.3/android_login_api/include/get_post.php");
                    } else {
                        Toast.makeText(MainActivity.this, "Network isn't available", Toast.LENGTH_LONG).show();
                    }

                    updateDisplay();
                    adapter.notifyDataSetChanged();


                }
            }
        }

and my updateDisplay function:

protected void updateDisplay() {
     adapter = new MyCustommAdapter(MainActivity.this, R.layout.list_item, postList);

       // adapter = new MyCustommAdapter(MainActivity.this, R.layout.list_item, postList);
    adapter.notifyDataSetChanged();
    myList.setAdapter(adapter);

}

my requestData() function:

 private void requestData(String uri) {


        swipeRefreshLayout.setRefreshing(true);
        StringRequest request = new StringRequest(uri,

                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        postList = PostJSONParser.parseFeed(response);

                        updateDisplay();
                        adapter.notifyDataSetChanged();
                        swipeRefreshLayout.setRefreshing(false);
                    }

                },

                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError ex) {
                        Toast.makeText(MainActivity.this, ex.getMessage(), Toast.LENGTH_LONG).show();

                        swipeRefreshLayout.setRefreshing(false);
                    }
                });

        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(request);
    }

PostJSONParser class:

public class PostJSONParser {


    public static List<Post> parseFeed(String content){


        JSONArray ar = null;
        try {
            ar = new JSONArray(content);
            List<Post> postList = new ArrayList<Post>();
            for (int j =0; j<ar.length(); j++){
                JSONObject obj = ar.getJSONObject(j);
                Post post = new Post();

                post.setId(obj.getInt("id"));
                post.setTitle(obj.getString("title"));
                post.setContent(obj.getString("content"));
                post.setCreated_at(obj.getString("created_at"));
                post.setUrl_image(obj.getString("url_image"));
                postList.add(post);

            }
            return postList;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }


    }
}

notice that requestData() function get new item using volley.

Any Idea how Implement it?

  • In your requestData(...) function are you updaing postList variable ? because I think you are overwriting the (ArrayList) postList with new data instead of just adding postList.add(newItem); – Sanjeev Aug 12 '15 at 15:34
  • I'm added requestData() function. – Khaled Rostampour Aug 12 '15 at 15:38
  • This is the problem postList = PostJSONParser.parseFeed(response); this line is overwriting this variable with new data , so your previous data is getting lost – Sanjeev Aug 12 '15 at 15:42
  • I updated the answer can you try the new one – Sanjeev Aug 12 '15 at 15:59

2 Answers2

1

I'm not sure if I understand you question fully, however when trying to create an "endless list" I have always found that the best structure to use is an ArrayList, it has preset functions for adding and deleting data that I found very useful. http://developer.android.com/reference/java/util/ArrayList.html

James Wolfe
  • 340
  • 5
  • 18
0

UPDATED Here's the solution Create a new ArrayList variable that will hold new data Temporarily and then add it this ArrayList to the original use ArrayList everwhere

First where you have declare the variable postList make sure it is like this

 ArrayList<Post> postList=new ArrayList<Post>(); //and make this variable global 

Second in your parseFeed function

public class PostJSONParser {


public static ArrayList<Post> parseFeed(String content){


    JSONArray ar = null;
    try {
        ar = new JSONArray(content);
        ArrayList<Post> postList = new ArrayList<Post>();
        for (int j =0; j<ar.length(); j++){
            JSONObject obj = ar.getJSONObject(j);
            Post post = new Post();

            post.setId(obj.getInt("id"));
            post.setTitle(obj.getString("title"));
            post.setContent(obj.getString("content"));
            post.setCreated_at(obj.getString("created_at"));
            post.setUrl_image(obj.getString("url_image"));
            postList.add(post);

        }
        return postList;
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }


}
}

And finally inside your requestData() function

private void requestData(String uri) {


    swipeRefreshLayout.setRefreshing(true);
    StringRequest request = new StringRequest(uri,

            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    ArrayList<Post> tempList=new ArrayList<Post>();
                    tempList = PostJSONParser.parseFeed(response);
                    postList.addAll(tempList);

                    updateDisplay();
                    adapter.notifyDataSetChanged();
                    swipeRefreshLayout.setRefreshing(false);
                }

            },

            new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError ex) {
                    Toast.makeText(MainActivity.this, ex.getMessage(), Toast.LENGTH_LONG).show();

                    swipeRefreshLayout.setRefreshing(false);
                }
            });

    RequestQueue queue = Volley.newRequestQueue(this);
    queue.add(request);
}

Make sure you use ArrayList everwhere

Community
  • 1
  • 1
Sanjeev
  • 4,255
  • 3
  • 28
  • 37
  • I added to requestData() function but got an error: java.lang.NullPointerException – Khaled Rostampour Aug 12 '15 at 15:49
  • can you try this postList.addAll(PostJSONParser.parseFeed(response)); and remove all the new line that i just added just use this one line – Sanjeev Aug 12 '15 at 15:56
  • I add parser. I try last code bu again java.lang.NullPointerException error on that line – Khaled Rostampour Aug 12 '15 at 16:01
  • I thin I found some thing. the problem is I use both first time and for updating list just one code. I create same function with different name and use your code. it's work but when load new item it's go to first item. how fix it? – Khaled Rostampour Aug 12 '15 at 16:06
  • This is another problem , maybe this will work [SO](http://stackoverflow.com/questions/11877848/android-listview-scrolls-back-to-top-on-update) – Sanjeev Aug 12 '15 at 16:15