Endless Listview with current Async Task

2019-07-17 07:01发布

问题:

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;



      }

      }