Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling notifyDataSetChanged from inside adapter fails

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;

}

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

Any insight is appreciated!

Thank you.

like image 488
user1791622 Avatar asked Nov 17 '12 02:11

user1791622


People also ask

What happens when we call notifyDataSetChanged?

notifyDataSetChanged() was called. all Adapter subclasses Adapter reference must call notifyDataSetChanged() when their underlying data has changed, to let the observing view (ListView and such) know that it should refresh itself because the data changed / there is new data.

What is notifyDataSetChanged in ListView?

It tells the ListView that the data has been modified; and to show the new data, the ListView must be redrawn.


1 Answers

Just add

notifyDataSetChanged()

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

^^

like image 142
Terranology Avatar answered Oct 15 '22 10:10

Terranology