M
M
Mr Mojito2019-04-14 11:34:05
Java
Mr Mojito, 2019-04-14 11:34:05

How to force ListView to load items when scrolling down?

There is a Json file that needs to be loaded into the ListView, but it is only necessary that the first 10 items are loaded into it, and when scrolling down, the next 10 items are displayed. At first, the first 10 elements are loaded, and then the 91st element abruptly goes and it repeats 9 times

public class MainActivity extends Activity implements download_complete {

  public ListView list;
    public static  ArrayList<Articles> countries = new ArrayList<Articles>();
    public static ListAdapter adapter;
    public static int clicked;
  boolean isLoading=false;
  int next;
  Articles add;
  JSONObject obj2;
  JSONArray data_array;


  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    list = (ListView) findViewById(R.id.list);			
    adapter = new ListAdapter(this);

    
    Download_data download_data = new Download_data((download_complete) this);
    download_data.download_data_from_link("http://jsonplaceholder.typicode.com/posts");

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View itemClicked, int position,
                  long id) {


                Intent intent = new Intent(MainActivity.this, Article.class);
                startActivity(intent);
                clicked = position;


      }
    });
    list.setOnScrollListener(new AbsListView.OnScrollListener(){

      @Override

      public void onScrollStateChanged(AbsListView view, int scrollState) {

        // Ignore this method

      }



      @Override

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

        Log.i("Main",totalItemCount+"");



        int lastIndexInScreen = visibleItemCount + firstVisibleItem;



        if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
        {
          if(isLoading == false)
          {
            isLoading = true;
            loadMore();
          }
        }
        adapter.notifyDataSetChanged();


      }

    });
    list.setAdapter(adapter);
  }




  public void get_data(String data)
  {

    try {
      int i;
       data_array=new JSONArray(data);



      for ( i=0 ; i <=10 ; i++) {

          JSONObject obj = new JSONObject(data_array.get(i).toString());

         add = new Articles();
        add.title = obj.getString("title");
        add.body = obj.getString("body");




          countries.add(add);


        }
        next=i;



      adapter.notifyDataSetChanged();

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

  }
  public void loadMore() {

    int i;
    try {


      if (countries.size() <= 90) { // Limit the number of items to 100 (stop loading when reaching 100 items)

        for (i = next; i <= next +9; i++)
          obj2 = new JSONObject(data_array.get(i).toString());
          add.title = obj2.getString("title");
          add.body = obj2.getString("body");
          countries.add(add);

        // Notify the ListView of data changed

        adapter.notifyDataSetChanged();

        isLoading = false;

        // Update next

        next = i;

      }


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

  }

It turns out this:
5cb2f04413004009122988.gif
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly Zharov, 2019-04-14
@SeaBreeze876

for (i = next; i <= next +9; i++)
    obj2 = new JSONObject(data_array.get(i).toString());
    add.title = obj2.getString("title");
    add.body = obj2.getString("body");
    countries.add(add);

this code is obviously incorrect
, I recommend pressing the ctrl + alt + l combination more often, and there will be fewer such errors

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question