如何滚动时加载从服务器到列表视图的数据? [英] How to load data from server to listview when scrolled?

查看:100
本文介绍了如何滚动时加载从服务器到列表视图的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图这显示图像和文字。我想显示8行的ListView同时从服务器下载数据。当用户滚动列表视图,我需要下载更多更多的数据表单服务器,并显示在列表视图中的项目。我已经使用的AsyncTask从服务器下载数据。

I have a listview which displays a image and a text. I want display 8 listview rows at a time downloading the data from the server. When the user scrolls the listview i need to download more more data form server and display the items in listview. I have used AsyncTask to Download data from server.

@Override
    protected Void doInBackground(Void... params) {
        getData();// get data first time. 8 data items.
        return null;
    }

   @Override
   protected void onPostExecute(Void result) {

        super.onPostExecute(result);
        pd.dismiss();
        lv= (ListView) findViewById(R.id.lvn);
        yt = new YouTubeAdapter(Youtube.this,msg,title,thumb);
        lv.setAdapter(yt);
        lv.setOnScrollListener(new EndLessScroll());

    }

获取数据code

Get Data Code

   public void getData()
{
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpGet request = new HttpGet("http://gdata.youtube.com/feeds/api/users/mbbangalore/uploads?v=2&alt=jsonc");     
    try
    {
    HttpResponse response = httpclient.execute(request);
    HttpEntity resEntity = response.getEntity();
    String _response=EntityUtils.toString(resEntity); // content will be consume only once

    JSONObject json = new JSONObject(_response);

    jsonArray = json.getJSONObject("data").getJSONArray("items");
    for (int i = 0; i < 8; i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);

        String title1 = jsonObject.getString("title");
        title.add(title1);
        String thumbUrl = jsonObject.getJSONObject("thumbnail").getString("sqDefault");
        URL url1 = new URL(thumbUrl);
        Bitmap bmp = BitmapFactory.decodeStream(url1.openConnection().getInputStream());
        thumb.add(bmp);
        String url;
        try {

            url = jsonObject.getJSONObject("player").getString("default");
            msg.add(url);
        } catch (JSONException ignore) {
        }
    }
    } 
    catch(Exception e1)
        {
            e1.printStackTrace();
        }

    httpclient.getConnectionManager().shutdown();
}

加载数据的时候滚动

Loading Data when Scrolling

  class EndLessScroll implements OnScrollListener {

    private int visibleThreshold = 5;
    private int currentPage = 0;
    private int previousTotal = 0;
    private boolean loading = true;

    public EndLessScroll() {
    }
    public EndLessScroll(int visibleThreshold) {
        this.visibleThreshold = visibleThreshold;
    }

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

         if (lv.getLastVisiblePosition() >= lv.getCount());
             {System.out.println("............................"+"first"+ firstVisibleItem+"visible"+visibleItemCount+"total"+ totalItemCount);
               for (int i= totalItemCount; i < jsonArray.length(); i++) {
                   try
                   {
                   JSONObject jsonObject = jsonArray.getJSONObject(i);
                   // The title of the video
                   String title1 = jsonObject.getString("title");
                   title.add(title1);
                   System.out.println("title"+title);
                   String thumbUrl = jsonObject.getJSONObject("thumbnail").getString("sqDefault");
                   URL url1 = new URL(thumbUrl);
                   Bitmap bmp = BitmapFactory.decodeStream(url1.openConnection().getInputStream());
                   thumb.add(bmp);
                   String url;
                    url = jsonObject.getJSONObject("player").getString("default");
                       msg.add(url);
                   } catch (JSONException ignore) {
                     //  url = jsonObject.getJSONObject("player").getString("default");
                   }
                   catch(Exception e)
                   {

                   }
                   yt.notifyDataSetChanged();
               }
         }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }
}

现在的问题。我可以显示滚动列表视图8行。想象一下服务器有100​​00数据项。我需要显示8项列表视图在同一时间下载休息时,用户向下滚动。如,当用户向下滚动新数据应被下载并显示,直到用户能够看到第一万数据。有什么不对的code 17。

Now the Problem. I can display 8 listview rows with scroll. Imagine the server has 10000 data items. I need to display 8 items in listview at a time downloading the rest when user scrolls down. As and when user scrolls down new data should be downloaded and displayed till user is able to see the 10000th data. What's wrong with this code??.

推荐答案

如果你不想使用滚动检测,另一种方式是从适配器,当最后一个项目是从 getView请求触发下载(),运行任务,并追加一些更多的项目适配器的集合。然后调用 notifyDataSetChanged()

If you don't want to use scrolling detector, Another way is to trigger download from adapter, when last item is requested from getView(), run task and append some more items to Adapter's collection. Then call notifyDataSetChanged().

更新:

请参阅演示要点这里

这篇关于如何滚动时加载从服务器到列表视图的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆