开发者

Some items are null in listview onScroll method

I have a listview and I override the onScroll event for it so that I can get the first character of the text on the first visible item of the listview. My code is as follows:

@Override
public void onScrollStateChanged(AbsListView view开发者_运维百科, int scrollState) {
    //
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
           int visibleItemCount, int totalItemCount) {
    ListView caller = (ListView) view;
    View v = caller.getChildAt(firstVisibleItem);
    if(v instanceof TextView){
        TextView tv = (TextView) v;
        if(tv != null){
            String sInitial = tv.getText().toString(); 
            sInitial = Character.toString(sInitial.charAt(0));

            TextView tvPager = (TextView) findViewById(R.id.tvPager);
            tvPager.setText(sInitial);
        }
    }
}

When the FirstVisibleItem variable is from 0 to 12(to be precise), my View v is not null and I can get the text of it. But when it goes beyond 12, my v is already null. My items are way more than 12 so it shouldn't be null.

Is there something wrong with my code? Or are there better way doing what I want? Thanks in advance!


An android listview recycles "items" in the list when they are not visible on the screen. So anything that is not visible will be null.

http://commonsware.com/Android/excerpt.pdf

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜