There is a Json file that needs to be loaded into the ListView, but you only need to load the first 10 items into it, and when scrolling down, the next 10 items were displayed.At first, the first 10 elements are loaded, and then the 91st element goes abruptly and it repeats 9 times.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 that:


What am I doing wrong?

1 Answers 1

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 to press the ctrl + alt + l combination more often, and there will be fewer such errors
  • I have a poppy, what is this combination for? – Upset Unicorn Apr 14 '19 at 13:39
  • [[replicate]], auto-formatting code – Hip-Hop43 Apr 14 '19 at 13:56