如何在没有OnScrollListener的情况下使用Async在android中的listview中显示大量数据

时间:2013-12-10 10:37:00

标签: android listview android-listview android-asynctask

我想使用Async在android listview中显示大量数据(+50,000条记录)。

数据来自页面中的Web服务(点网)(每页1000条记录)。 当我获得1000条记录时,我必须自动更新列表视图(不滚动)。这个过程一直持续到获取所有记录。

能够获取所有记录但无法更新列表视图。

我的代码是:

class XYZ extends AsyncTask<String, Integer, String> 
{

    @Override
    protected String doInBackground(String... params) {

        for(int i=1;i<=noOfPagesFromServer;i++)
        {   
            String url="http://182.72.123.138:9523/Service.svc/GetData/"+i;  

            try
            {
                HttpGet get =new HttpGet(url );
                HttpClient httpClient = new DefaultHttpClient();
                HttpResponse httpResponse = httpClient.execute(get);
                BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
                StringBuilder stringBuilder = new StringBuilder();
                String line = null;
                while((line = reader.readLine()) != null)
                {
                    stringBuilder.append(line + "\n");
                }
                String responseString = stringBuilder.toString();
                JSONObject serverJSONObj = new JSONObject(responseString);
                JSONArray serverJSONArray = serverJSONObj .getJSONArray("ABC");
                for(int l=0;l<serverJSONArray.length();l++)
                {
                    JSONObject tempJSONObject=serverJSONArray.getJSONObject(l);
                    a   = tempJSONObject.getString("A");
                    b   =tempJSONObject.getString("B");
                    Model model=new Model(a,b);

                    arrayList.add(model);}
            } catch (Exception e) {

                e.printStackTrace();
            }
            publishProgress(null);
            SystemClock.sleep(6000);
        }return null;}



          protected void onProgressUpdate(Integer... values) 
    { 

        super.onProgressUpdate(values); 
        adapter1 = new CustomListViewAdapter(SearchActivity.this,R.layout.row,arrayList); 
        listView.setAdapter(adapter1); 
        listView.this.adapter1.notifyDataSetChanged(); 
    }  


    @Override
    protected void onPostExecute(String result) {

        try{
            super.onPostExecute(result);

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {


                        adapter1 = new CustomListViewAdapter(SearchActivity.this,R.layout.row,arrayList);
                    adapter1.notifyDataSetChanged();
                    listView.setAdapter(adapter1);
                    }
                });

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

        }
    }

感谢您的回复

1 个答案:

答案 0 :(得分:0)

您可以使用onProgressUpdate()并获得所需的结果...一旦收到1000条记录并更新列表,就更新您的模型......