从内部适配器调用notifyDataSetChanged失败 [英] Calling notifyDataSetChanged from inside adapter fails

查看:138
本文介绍了从内部适配器调用notifyDataSetChanged失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个适配器(扩展BaseAdapter),我试图调用notifyDataSetChanged(),但它不起作用。我相信notifyDataSetChanged()实际上是被调用的,基于使用Eclipse调试器踩到它。但是,它不会调用getView()。我的适配器下面的数据正在改变,正如它应该的那样。我在适配器中有一个方法updateSettings(),用于更新适配器底层的数据。如果我从ListViewActivity调用updateSettings(),我没有任何问题... notifyDataSetChanged()的行为与我期望的一样。但是当我从适配器中调用updateSettings()时,notifyDataSetChanged()不起作用。我是否尝试从适配器中调用notifyDataSetChanged()来做一些根本错误的事情?以下是我的一些代码:

I have an adapter (extends BaseAdapter) from which I'm trying to call notifyDataSetChanged(), but it's not working. I believe notifyDataSetChanged() is actually being called, based on stepping into it using the Eclipse debugger. However, it doesn't call getView(). The data underlying my adapter is being changed, as it should. I have a method, updateSettings(), in the adapter that updates the data underlying the adapter. If I call updateSettings() from my ListViewActivity, I don't have any problem...notifyDataSetChanged() behaves as I expect. But when I call updateSettings() from within the adapter, notifyDataSetChanged() doesn't work. Am I doing something fundamentally wrong by trying to call notifyDataSetChanged() from within an adapter? Here is some code I have:

public View getView( int position, View convertView, ViewGroup parent )
{
    Log.d( CLASS_NAME, "Entered getView()" );
    View row = convertView;
    _settingViewHolder = new PhoneSettingViewHolder();
    final int index = position;

    /* If this is the first time the list is being built, get all of the UI widgets
     * and store them in a PhoneSettingViewHolder object for future use on
     * subsequent writing of the list.
     */
    if( row == null )
    {
        LayoutInflater inflater = (LayoutInflater)_context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        row = inflater.inflate( R.layout.phone_list_complex, null, false ); 

        _settingViewHolder.txtSettingName = (TextView)row.findViewById( R.id.phone_list_complex_title );
        _settingViewHolder.txtSettingValue = (TextView)row.findViewById( R.id.phone_list_complex_caption );
        _settingViewHolder.sbTimeout = (SeekBar)row.findViewById( R.id.slider );
        _settingViewHolder.txtPercent = (TextView)row.findViewById( R.id.percent );

        _settingViewHolder.sbTimeout.setMax( 30 );
        _settingViewHolder.sbTimeout.setProgress( 1 );

        // associate this PhoneSettingViewHolder object with the row
        row.setTag( _settingViewHolder );
    }
    else
    {
        // get the PhoneSettingViewHolder object that is associated with this row
        _settingViewHolder = (PhoneSettingViewHolder)row.getTag();
    }

    // get Setting object and store values in PhoneSettingViewHolder object
    final Setting setting = _settings.get( position );
    _settingViewHolder.txtSettingName.setText( setting.getSettingName() );
    Log.d( CLASS_NAME, "setting.getSettingValue() = " + setting.getSettingValue() );
    _settingViewHolder.txtSettingValue.setText( setting.getSettingValue() );

    /* Event handlers for SeekBar */
    _settingViewHolder.sbTimeout.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
        public void onProgressChanged( SeekBar seekBar, int progress, boolean isUser )
        {
            Log.d( CLASS_NAME, "Entered onProgressChanged()" );
            String percent = String.valueOf( progress );
            updateSettings( index, percent );
        }

        public void onStartTrackingTouch( SeekBar seekBar )
        {
            // Nothing to do here
        }

        public void onStopTrackingTouch( SeekBar seekBar )
        {
            // Nothing to do here 
        }
    });


    return row;

}

在此块中,我设置了行视图并附加SeekBar的事件处理程序。我想要发生的是当有人滚动SeekBar时,它会更新它上面的文本。这是我的updateSettings()方法:

In this block, I set up the row view and attach event handlers to the SeekBar. What I want to happen is that when someone scrolls the SeekBar, it updates the text above it. Here is my updateSettings() method:

    public void updateSettings( int index, String progress ) 
    {
        Log.d( CLASS_NAME, "Entered updateSettings()" );
        Setting setting = new Setting( SETTING_NAME, progress );
        _settings.set( index, setting );
        Log.d( CLASS_NAME,  "Calling notifyDataSetChanged()" );
        notifyDataSetChanged();
        Log.d( CLASS_NAME, "Returned from notifyDataSetChanged()" );
    }

非常感谢任何见解!

谢谢。

推荐答案

添加


notifyDataSetChanged()

notifyDataSetChanged()

**在你的适配器中,应用程序会重新加载你的列表。

** inside your Adapter and the app'll reload your list.

^^

这篇关于从内部适配器调用notifyDataSetChanged失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆