title: Java Android ListView add item
body: <p>I want to implement <code>Load-More</code> feature in my <code>ListView</code>. </p>
  
  <p><strong>Here is my code:</strong></p>
  
  <pre><code>public class MainActivity extends Activity {
  
      int url_page = 0;
      private static String URL = "http://MyURL&amp;page_no=";
      ArrayList&lt;ItemInfo&gt; products = new ArrayList&lt;ItemInfo&gt;();
      BoxAdapter boxAdapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          new AsyncTaskParseJson().execute();
      }
  
  
      public class AsyncTaskParseJson extends AsyncTask&lt;String, String, String&gt; {
  
         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 &lt; 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&lt;?&gt; 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 &gt;= 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;
      }
  
  }
  </code></pre>
  
  <p><strong>I added this to onScroll event:</strong></p>
  
  <pre><code>if (lvMain.getLastVisiblePosition() &gt;= lvMain.getCount())
                      {
                          url_page = url_page + 1;
                          new LoadLastestPost().execute();
                      }
  </code></pre>
  
  <p><strong>And asyntask:</strong></p>
  
  <pre><code>public class LoadLastestPost extends AsyncTask&lt;String, String, String&gt; {
  
             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 &lt; 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();
      }
  </code></pre>
  
  <p>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.</p>