I want to dynamically load a ListView, for example, load them during the scrolling so its not loading all 100 posts I have. How can I achieve this?
I have looked at similiar solutions here at SO, but since I not got it to work, I asked this question.
My code:
//orders is my is my ArrayList<WithMyOwnOrderClass> that handling orders
/* adapter is my inner private class in this piece of code, that trigging
the getView */
if(orders != null && orders.size() > 0){
for(int i=0;i<orders.size();i++){
adapter.add(orders.get(i));
}
adapter.notifyDataSetChanged();
progressDialog.dismiss();
}
I have in the same .java file, functions to download the info from the web and loop through 100 items, like this:
JSONObject jObject = new JSONObject(queryResult);
SONArray docsArray = jObject.getJSONArray("array");
for (int i = 0; i < 100; i++) {
开发者_C百科 try {
title = docsArray.getJSONObject(i).optString("title");
} catch (JSONException e) {}
}
and then it add a new order correctly and so on. But now!(?) I want to have so when the first item is loaded, it should appear and when scrolling it loads gradually.
Hope that this can be achieved and thanks in advance for any help!
I am going to assume, that you are getting the JSON of the items from a single Web service call or something.
If so, do your query-and-JSON-parsing in an AsyncTask
. Have your implementation of doInBackground()
call publishProgress()
with each item. Have your implementation of onProgressUpdate()
call add()
on an ArrayAdapter
(on an ArrayList<>
) to append the item to the ListView
. Here is a sample project demonstrating the technique.
精彩评论