android – 如何在滚动时将数据从服务器加载到listview?

前端之家收集整理的这篇文章主要介绍了android – 如何在滚动时将数据从服务器加载到listview?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个列表视图显示一个图像和一个文本.我想要一次从服务器下载数据时显示8个listview行.当用户滚动列表视图时,我需要下载更多的数据表服务器,并在列表视图中显示项目.我使用AsyncTask从服务器下载数据.
@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());

    }

获取数据代码

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();
}

滚动时加载数据

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个listview行.想象一下,服务器有10000个数据项.当用户向下滚动时,我需要在列表视图中显示8项.当用户滚动下来时,应该下载和显示新数据,直到用户能够看到第10000个数据.这段代码怎么了?

解决方法

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

更新:

参见演示要点here.

原文链接:https://www.f2er.com/android/311637.html

猜你在找的Android相关文章