7

I am developing an app where I need to show some list view control. I am just implementing the OnScrollListener in my activity in order to monitor my listview content but it's not invoking the onScroll event when I set the layout for my activity. If i comment that block then it invokes it just fine. How do I solve this issue?

package com.Threadtest;

import com.Threadtest.main.myRunnable;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;

public class TestList extends ListActivity implements OnScrollListener {

    Aleph0 adapter = new Aleph0();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        ListView listview=(ListView)findViewById(android.R.id.list);
        listview.setAdapter(adapter);
        /*setListAdapter(adapter); 
        getListView().setOnScrollListener(this);*/
    }

    public void onScroll(AbsListView view,
        int firstVisible, final int visibleCount, int totalCount) {

        Log.i("TestList", "firstVisible="+firstVisible+" visibleCount="+visibleCount+" totalCount="+totalCount);
        boolean loadMore = /* maybe add a padding */
            firstVisible + visibleCount >= totalCount;

        if(loadMore) {
            Log.i("TestList", "In Notify");
            Thread thread = new Thread()
            {
                  @Override
                  public void run() {
                      try {
                        this.sleep(10000);
                        runOnUiThread(new myRunnable(adapter,visibleCount));
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


                  }
              };

            thread.start();

        }
    }

    public void onScrollStateChanged(AbsListView v, int s) { } 

    class myRunnable implements Runnable
    {
        Aleph0 adapter;

        public myRunnable(Aleph0 adapter,int visibleCount)
        {

            this.adapter=adapter;
            this.adapter.count += visibleCount;
        }
        public void run() {
            // TODO Auto-generated method stub
             Log.i("TestList", "List Count="+adapter.count);
                adapter.notifyDataSetChanged();
        }

    }

    class Aleph0 extends BaseAdapter {
        int count = 40; /* starting amount */

        public int getCount() { return count; }
        public Object getItem(int pos) { return pos; }
        public long getItemId(int pos) { return pos; }

        public View getView(int pos, View v, ViewGroup p) {
            if(pos!=count-1)
            {
                TextView view = new TextView(TestList.this);
                view.setText("entry " + pos);
                return view;
            }
            TextView view = new TextView(TestList.this);
            view.setText("Loading.. " + pos);
            return view;

        }
    }
}
dmon
  • 30,048
  • 8
  • 87
  • 96
ram
  • 3,487
  • 10
  • 33
  • 47
  • 1
    I'm confused. Did you mean to say that if you omit `getListView().setOnScrollListener(this)`, it doesn't work? – dmon Jun 15 '11 at 12:36
  • Yes. If a Ommit the getListView().setOnScrollListener(this) It's Not getting fired. – ram Jun 15 '11 at 12:45
  • Right, if you omit listener, it will not fire. What is there real problem, **you want to fire without setting listener?!** – Labeeb Panampullan Jun 15 '11 at 12:56

4 Answers4

17

getListView().setOnScrollListener(this) is necessary to bind the listener to the ListView. The ListActivity class does not provide an "automatic" way of binding most of the listeners, It only provides an automatic way of binding the OnItemClickListener. Check out the ListActivity code.

dmon
  • 30,048
  • 8
  • 87
  • 96
1

If you change your onScroll signature to public void onScroll(AbsListView view, int firstVisible, int visibleCount, int totalCount) will it be called? By adding the final qualifier, you've changed the method signature and this will cause your overridden method to not be seen. Copy the visibleCount int to a new (final) int so you can access it in your thread.

mah
  • 39,056
  • 9
  • 76
  • 93
1
import android.widget.AbsListView.OnScrollListener;

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.test); 

        ListView listview=(ListView)findViewById(android.R.id.list); 
        listview.setAdapter(adapter); 
        listview.setOnScrollListener(onLstScroll);    
} 


OnScrollListener onLstScroll= new OnScrollListener()
{
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
   {
       //Do something
   }

   @Override
   public void onScrollStateChanged(AbsListView view, int scrollState)
   {
      //Do something
   }
};
Forrest
  • 11
  • 1
-4

You're missing the @Override notation on the onScroll() and onScrollStateChanged() methods.

Andre
  • 3,150
  • 23
  • 23
  • @Override doesn't get compiled into byte code. It just states that you *intended* to override something, so the compiler can tell you if you didn't. – dmon Jun 15 '11 at 12:33
  • @dmon Please see this question to see why it's required in order for the method to be called (relates to Android and Activities) http://stackoverflow.com/questions/2950074/android-override-usage – Andre Jun 15 '11 at 12:38
  • Maybe *you* should re-read that answer, which states exactly what I commented on your answer. – dmon Jun 15 '11 at 12:49
  • @dmon -- if the OP included the @Override notation, the compiler would have told him that in fact he's not overriding a method, because he changed the original method's signature by adding the final qualifier. Andre's answer is valid and helpful, even if not explicitly explaining why. – mah Jun 15 '11 at 12:52
  • @mah the final is not the problem. `OnScrollListener` is an interface, the code would not compile *at all* if he was not properly overriding the methods. – dmon Jun 15 '11 at 12:56