具有当前异步任务的无尽列表视图

发布于 2024-11-17 03:12:04 字数 4220 浏览 2 评论 0原文

我仔细研究了 CWAC Endless 适配器和其他几个做同样事情的适配器,但我无法弄清楚如何将它们应用到我当前的检索数据的方法中,该方法填充 2 行和一个列表视图。目前,数据是通过 ASync 后台任务中的 httpget 检索的。稍微更改 url 即可获取每个后续​​页面(page=1.html、page=2.html 等)。

我希望在到达页面末尾时加载第 2 页并附加到第 1 页的末尾 - 我想可以使用 CWAC 方法,但我不知道如何实现。下面是我的代码,非常感谢帮助。

我当前的系统对于显示单个页面来说工作得非常好 - 但我想要无限的列表加载。

private class MyTask extends AsyncTask<Void, Void, Void> {  



        private ProgressDialog progressDialog;
        protected void onPreExecute() {
               {progressDialog = ProgressDialog.show(TwitterPage.this,
                                  "", "Loading. Please wait...", true);}

        }


        @Override
        protected Void doInBackground(Void... arg0) {

            SharedPreferences myPrefs = getPreferences(MODE_PRIVATE);
                try {   

                    String twitPlayers = getString(R.string.twit_players);
                    String twitClub = getString(R.string.twit_club);
                    String twitAndr = getString(R.string.twit_andr);
                    String twitMentions = getString(R.string.twit_mentions);
                    HttpClient hc;
                    HttpGet get;
                    HttpResponse rp;
                    if (myPrefs.getInt("FILTER_INT", 1) == 1){
                        hc = new DefaultHttpClient();
                        get = new
                        //All
                        HttpGet("http://search.twitter.com/search.json?q=" + twitMentions + "+OR+" + twitAndr + "+OR+from" + twitPlayers + "+OR+from" + twitClub + "&result_type=recent&rpp=15&page="+currentpage);
                        rp = hc.execute(get);}
                    else if (myPrefs.getInt("FILTER_INT", 1) == 2){
                        hc = new DefaultHttpClient();
                        get = new

         ........LOADS MORE IF STATEMENTS REMOVED AS NOT RELEVANT............


                        rp = hc.execute(get);}

                    if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
                        {
                                String result = EntityUtils.toString(rp.getEntity());
                                JSONObject root = new JSONObject(result);
                                JSONArray sessions = root.getJSONArray("results");
                                for (int i = 0; i < sessions.length(); i++) {
                                        JSONObject session = sessions.getJSONObject(i);
                                Tweet tweet = new Tweet();
                                         tweet.content = session.getString("text");
                                         tweet.author = session.getString("from_user");
                                         tweets.add(tweet);
                                }
                       }
               } catch (Exception e) {
                       Log.e("TwitterFeedActivity", "Error loading JSON", e);

               }




               return null;
      }
      @Override
      protected void onPostExecute(Void result) {
      {  progressDialog.dismiss();}



          //setadapter
          setListAdapter(new TweetListAdaptor(
                  TwitterPage.this, R.layout.list_item, tweets));


     }
    }



     private class TweetListAdaptor extends ArrayAdapter<Tweet> {
      private ArrayList<Tweet> tweets;
      public TweetListAdaptor(Context context,
      int textViewResourceId,
      ArrayList<Tweet> items) {
      super(context, textViewResourceId, items);
      this.tweets = items;
      }
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
              View v = convertView;
              if (v == null) {
                      LayoutInflater vi = (LayoutInflater)
      getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                      v = vi.inflate(R.layout.list_item, null);
              }
              Tweet o = tweets.get(position);
              TextView tt = (TextView) v.findViewById(R.id.toptext);
              TextView bt = (TextView) v.findViewById(R.id.bottomtext);
              tt.setText(o.content);
              bt.setText(o.author);
              return v;



      }

      }

I have had a good look at CWAC Endless adapter and a couple of others that do the same thing, but I cannot work out how to apply them to my current method of retrieving data, which populates 2 rows and a listview. Currently the data is retrieved via httpget in an ASync background task. The url is altered slightly to get each subsequent page (page=1.html, page=2.html etc).

I would like page 2 to load and append to the end of page 1 when the end is reached - I guess it is possible using CWACs method but I cannot work out how. Below is my code, help much appreciated.

My current system works absolutely fine for displaying a single page - but I would like the endless list loading.

private class MyTask extends AsyncTask<Void, Void, Void> {  



        private ProgressDialog progressDialog;
        protected void onPreExecute() {
               {progressDialog = ProgressDialog.show(TwitterPage.this,
                                  "", "Loading. Please wait...", true);}

        }


        @Override
        protected Void doInBackground(Void... arg0) {

            SharedPreferences myPrefs = getPreferences(MODE_PRIVATE);
                try {   

                    String twitPlayers = getString(R.string.twit_players);
                    String twitClub = getString(R.string.twit_club);
                    String twitAndr = getString(R.string.twit_andr);
                    String twitMentions = getString(R.string.twit_mentions);
                    HttpClient hc;
                    HttpGet get;
                    HttpResponse rp;
                    if (myPrefs.getInt("FILTER_INT", 1) == 1){
                        hc = new DefaultHttpClient();
                        get = new
                        //All
                        HttpGet("http://search.twitter.com/search.json?q=" + twitMentions + "+OR+" + twitAndr + "+OR+from" + twitPlayers + "+OR+from" + twitClub + "&result_type=recent&rpp=15&page="+currentpage);
                        rp = hc.execute(get);}
                    else if (myPrefs.getInt("FILTER_INT", 1) == 2){
                        hc = new DefaultHttpClient();
                        get = new

         ........LOADS MORE IF STATEMENTS REMOVED AS NOT RELEVANT............


                        rp = hc.execute(get);}

                    if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
                        {
                                String result = EntityUtils.toString(rp.getEntity());
                                JSONObject root = new JSONObject(result);
                                JSONArray sessions = root.getJSONArray("results");
                                for (int i = 0; i < sessions.length(); i++) {
                                        JSONObject session = sessions.getJSONObject(i);
                                Tweet tweet = new Tweet();
                                         tweet.content = session.getString("text");
                                         tweet.author = session.getString("from_user");
                                         tweets.add(tweet);
                                }
                       }
               } catch (Exception e) {
                       Log.e("TwitterFeedActivity", "Error loading JSON", e);

               }




               return null;
      }
      @Override
      protected void onPostExecute(Void result) {
      {  progressDialog.dismiss();}



          //setadapter
          setListAdapter(new TweetListAdaptor(
                  TwitterPage.this, R.layout.list_item, tweets));


     }
    }



     private class TweetListAdaptor extends ArrayAdapter<Tweet> {
      private ArrayList<Tweet> tweets;
      public TweetListAdaptor(Context context,
      int textViewResourceId,
      ArrayList<Tweet> items) {
      super(context, textViewResourceId, items);
      this.tweets = items;
      }
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
              View v = convertView;
              if (v == null) {
                      LayoutInflater vi = (LayoutInflater)
      getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                      v = vi.inflate(R.layout.list_item, null);
              }
              Tweet o = tweets.get(position);
              TextView tt = (TextView) v.findViewById(R.id.toptext);
              TextView bt = (TextView) v.findViewById(R.id.bottomtext);
              tt.setText(o.content);
              bt.setText(o.author);
              return v;



      }

      }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文