dschuett -1 Junior Poster

I am looking for a way to impliment an onScroll listener into my listview. I have read a lot about using:

notifyDataSetChanged();

But, I honestly can't figure out how to impliment it into my working code.

Currently, I pull a bunch of records into the listview from a MySQL database using JSON/PHP. Everything works fine, but I want to limit the number of records returned when the activity is first launched (which i know is done with the sql statement within the php script) and then when the user scrolls to have it load older posts and prepend them to the current listview. I have looked at NUMEROUS examples, but I can't find any that relate to my current structure.

Here is my current code:

public class LiveFeed extends SherlockListActivity {
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Used To Put Dark Icons On Light Action Bar
        boolean isLight = ThemeList.THEME == R.style.Theme_Sherlock;

        // Build Action Bar
        menu.add("Refresh")
            .setIcon(isLight ? R.drawable.ic_refresh : R.drawable.ic_refresh_inverse).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        menu.add("+ Add Post")
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        return true;
    }

    // Define Menu Item Actions
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        String menuItemTitle = item.getTitle().toString();
        if(menuItemTitle.equals("Refresh")){
            // Clear ArrayList
            posts.clear();
            // Re-Run AsyncTask
            new FeedTask().execute();
        }
        if(menuItemTitle.equals("+ Add Post")){
            Intent intent = new Intent(this, AddPost.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        return true;
    }

    private ArrayList<Feed> posts = new ArrayList<Feed>();

    // Define JSON Nodes
    private static final String TAG_ID = "id";
    private static final String TAG_POST = "post";
    private static final String TAG_TIME = "post_time";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setTheme(ThemeList.THEME);
        super.onCreate(savedInstanceState);

        // Load All Posts
        new FeedTask().execute();

        // Get ListView
        ListView lv = getListView();

        // Selecting Single List Item Starts EditPost Activity
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // getting values from selected ListItem
                String pid = ((TextView) view.findViewById(R.id.postid)).getText().toString();
                // Starting new intent
                Intent in = new Intent(getApplicationContext(), EditPost.class);
                // sending Post Id To EditPost Activity
                in.putExtra(TAG_ID, pid);
                // starting new activity and expecting some response back
                startActivityForResult(in, 100);
            }
        });
    }

    // Response from Edit Product Activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // If Result Code 100 (edited/deleted post) - Reload Screen
        if (resultCode == 100) {
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }
    }

    // AsyncTask To Pull In All Posts And Build ListView
    private class FeedTask extends AsyncTask<Void, Void, Void> {
        private ProgressDialog progressDialog;
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(LiveFeed.this,"", "Loading. Please wait...", true);
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://mysite/android/login/get_posts.php");
                HttpResponse httpResponse = httpClient.execute(httpPost);
                String result = EntityUtils.toString(httpResponse.getEntity());
                JSONArray jArray = new JSONArray(result);
                JSONObject json_data = null;
                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);
                    Feed feed = new Feed();
                    feed.id = json_data.getString(TAG_ID);
                    feed.post = json_data.getString(TAG_POST);
                    feed.post_time = json_data.getString(TAG_TIME);
                    posts.add(feed);
                }
            } 
            catch (Exception e){
                Log.e("ERROR", "Error loading JSON", e);
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            progressDialog.dismiss();
            setListAdapter(new FeedListAdaptor(LiveFeed.this, R.layout.feed, posts));
        }
    }

    private class FeedListAdaptor extends ArrayAdapter<Feed> {
        private ArrayList<Feed> posts;
        public FeedListAdaptor(Context context,
        int textViewResourceId,
        ArrayList<Feed> items) {
            super(context, textViewResourceId, items);
            this.posts = 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.feed, null);
            }
            Feed o = posts.get(position);
            TextView ht = (TextView) v.findViewById(R.id.postid);
            TextView tt = (TextView) v.findViewById(R.id.toptext);
            TextView bt = (TextView) v.findViewById(R.id.bottomtext);
            ht.setText(o.id);
            tt.setText(o.post);
            bt.setText(o.post_time);
            return v;
        }
    }

    // Define Variables Used To Build Feed
    public class Feed {
        String id;
        String post;
        String post_time;
    }
}