从内部适配器调用notifyDataSetChanged失败

时间:2012-11-17 02:24:47

标签: java android adapter

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

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()方法:

    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()" );
    }

非常感谢任何见解!

谢谢。

5 个答案:

答案 0 :(得分:12)

添加

  

notifyDataSetChanged()

**您的适配器内部,应用程序将重新加载您的列表。

^^

答案 1 :(得分:3)

我遇到同样的问题,解决方法是: 不要在适配器内调用NotifyDataSetChanged() 使用listView.getAdapter().notiftDataSetChanged()

进行调用

它会起作用。

答案 2 :(得分:1)

我只是添加

adapter.notifyDataSetChanged()

onBindViewHolder()中的onCLick,它对我有用〜

答案 3 :(得分:0)

您必须以这种方式致电notifyDataSetChanged()

ArrayAdapter<String> adapter; // where adapter - your adapter
adapter = new ArrayAdapter<String>(...);
adapter.notifyDataSetChanged();

答案 4 :(得分:0)

感谢所有关注我问题的人。我希望这个解决方案可以帮助其他人。

我使用适配器作为分隔列表,它管理列表不同部分的适配器。我遇到问题的适配器是托管适配器之一。

我所做的是制作一个回调函数(在Java中),用于处理滚动SeekBar时的情况。我用我的适配器注册了它,所以当它被调用时,它调用adapter.notifyDataSetChanged:

public void eventNotifier( InterestingEvent event )
{
    _event = event; // register event handler
}

class NotifyDataSetChangedEvent implements InterestingEvent {

    private final String CLASS_NAME = "NotifyDataSetChangedEvent";

    public void interestingEventHappened()
    {
        Log.d( CLASS_NAME, "Entered interestingEventHappened()");
        _adapter.notifyDataSetChanged();
    }
}

谢谢:)