Answer the question
In order to leave comments, you need to log in
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();
}
}
}
Answer the question
In order to leave comments, you need to log in
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);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question