0

I want to implement Load-More feature in my ListView.

Here is my code:

public class MainActivity extends Activity {

    int url_page = 0;
    private static String URL = "http://MyURL&page_no=";
    ArrayList<ItemInfo> products = new ArrayList<ItemInfo>();
    BoxAdapter boxAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new AsyncTaskParseJson().execute();
    }


    public class AsyncTaskParseJson extends AsyncTask<String, String, String> {

       final String TAG = "AsyncTaskParseJson.java";
       JSONArray dataJsonArr = null;

       @Override
       protected void onPreExecute() {}

       @Override
       protected String doInBackground(String... arg0) {

           try {


               JsonParser jParser = new JsonParser();
               JSONObject json = jParser.getJSONFromUrl(URL+"0");
               dataJsonArr = json.getJSONArray("data");

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

                   JSONObject c = dataJsonArr.getJSONObject(i);

                   String nickname = c.getString("nickname");
                   String description = c.getString("description");
                   products.add(new ItemInfo(nickname, description));
               }

           } catch (JSONException e) {
               e.printStackTrace();
           }

           return null;
       }

       @Override
       protected void onPostExecute(String strFromDoInBg) 
       {
            SetAdapter();
       }
   }

    public void SetAdapter()
    {
        boxAdapter = new BoxAdapter(this, products);
        ListView lvMain = (ListView) findViewById(R.id.list);
       lvMain.setOnItemClickListener(new OnItemClickListener() {

           @Override
           public void onItemClick(AdapterView<?> parent, View view,
                   int position, long id) 
           {
            ListView lvMain = (ListView) findViewById(R.id.list);
            int pos = lvMain.getPositionForView(view);
               ItemInfo p = boxAdapter.getItem(pos);
               //
               String username = p.username;
               String description = p.description;
               // Starting single contact activity
               Intent in = new Intent(getApplicationContext(),
                    OnClickActivity.class);
               in.putExtra("username", username);
               in.putExtra("description", description);
               startActivity(in);
           }
       });
        lvMain.setAdapter(boxAdapter);
        lvMain.setOnScrollListener(new OnScrollListener()
        {

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub
                    ListView lvMain = (ListView) findViewById(R.id.list);
                    if(firstVisibleItem + visibleItemCount >= totalItemCount) {
                        boxAdapter.loadAdditionalItems();
                    }
            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
            }

        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

I added this to onScroll event:

if (lvMain.getLastVisiblePosition() >= lvMain.getCount())
                    {
                        url_page = url_page + 1;
                        new LoadLastestPost().execute();
                    }

And asyntask:

public class LoadLastestPost extends AsyncTask<String, String, String> {

           final String TAG = "LoadLastestPost.java";
           JSONArray dataJsonArr = null;

           @Override
           protected void onPreExecute() {}

           @Override
           protected String doInBackground(String... arg0) {

               try {


                   JsonParser jParser = new JsonParser();
                   String newURL = URL + String.valueOf(url_page);
                   JSONObject json = jParser.getJSONFromUrl(newURL);
                   dataJsonArr = json.getJSONArray("data");

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

                       JSONObject c = dataJsonArr.getJSONObject(i);

                       String nickname = c.getString("nickname");
                       String description = c.getString("description");
                       products.add(new ItemInfo(nickname, description));
                   }

               } catch (JSONException e) {
                   e.printStackTrace();
               }

               return null;
           }

           @Override
           protected void onPostExecute(String strFromDoInBg) 
           {
               LoadMoreItem();
           }
       }

    private void LoadMoreItem()
    {
        ListView lvMain = (ListView) findViewById(R.id.list);
        boxAdapter = null;
        boxAdapter = new BoxAdapter(this, products);
        lvMain.setAdapter(boxAdapter);
        boxAdapter.notifyDataSetChanged();
    }

But it is not working ;( That needs fixing? P.S i display 10 item, and load more 10 response of an already loaded. I want load items in the end of listview.

5
  • can't undertand what is your problem Commented Apr 4, 2014 at 12:56
  • When you can getLastVisiblePosition() it can be that it returns numbers in range [0, n). Therefore, it is never greater or equal to n. Try adding logging to see where your app fails. Commented Apr 4, 2014 at 13:00
  • maybe you are talking about Lazy Loading: stackoverflow.com/questions/10696530/… Commented Apr 4, 2014 at 13:02
  • Not add new items, just scroll up and not scroll down. Commented Apr 4, 2014 at 13:09
  • I think you need a hand on Math looking at this lvMain.getLastVisiblePosition() >= lvMain.getCount(). position goes from 0 to count-1.. just like arrays and length Commented Apr 4, 2014 at 23:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.