带有当前异步任务的Endless Listview

时间:2011-06-27 12:38:39

标签: android listview twitter loading

我已经很好地了解了CWAC Endless适配器和其他几个做同样事情的人,但是我无法弄清楚如何将它们应用到我当前的检索数据的方法中,该方法填充了2行和listview。目前,在ASync后台任务中通过httpget检索数据。网址略有改动,以获取每个后续​​页面(page = 1.html,page = 2.html等)。

我希望第2页加载并在到达结尾时附加到第1页的末尾 - 我想可以使用CWACs方法,但我无法弄清楚如何。以下是我的代码,非常感谢。

我当前的系统对于显示单个页面非常好 - 但我希望加载无限列表。

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;



      }

      }

0 个答案:

没有答案