Khaled Rostampour
Khaled Rostampour

Reputation: 125

Add New Item to ListView For Implement Endless List In Android

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?

Upvotes: 0

Views: 102

Answers (2)

Sanjeev
Sanjeev

Reputation: 4375

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

Upvotes: 0

James Wolfe
James Wolfe

Reputation: 360

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

Upvotes: 1

Related Questions