11

画像とテキストを表示するリストビューがあります。サーバーからデータをダウンロードして、一度に 8 つのリストビュー行を表示したい。ユーザーがリストビューをスクロールすると、サーバーからさらに多くのデータをダウンロードし、アイテムをリストビューに表示する必要があります。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行のリストビューを表示できます。サーバーに 10000 個のデータ項目があるとします。ユーザーが下にスクロールしたときに残りをダウンロードして、リストビューに一度に8つのアイテムを表示する必要があります。ユーザーが下にスクロールすると、新しいデータがダウンロードされ、ユーザーが 10000 番目のデータを表示できるようになるまで表示されます。このコードの何が問題になっていますか??.

4

1 に答える 1

12

スクロール検出器を使用したくない場合は、別の方法として、アダプタからのダウンロードをトリガーし、最後のアイテムが からリクエストされたときにgetView()タスクを実行し、さらにいくつかのアイテムをアダプタのコレクションに追加します。次に、 を呼び出しますnotifyDataSetChanged()

アップデート:

ここでデモの要点を参照してください。

于 2013-01-26T09:52:08.207 に答える