问题
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.
回答1:
Just add
notifyDataSetChanged()
** inside your Adapter and the app'll reload your list.
^^
回答2:
The same problem I've had, the solution is :
Don't call NotifyDataSetChanged() inside the adapter
call it using the listView.getAdapter().notiftDataSetChanged()
it will work .
回答3:
I just add
adapter.notifyDataSetChanged()
inside onCLick in onBindViewHolder(), it's work for me~
回答4:
You must call notifyDataSetChanged() in this way:
ArrayAdapter<String> adapter; // where adapter - your adapter
adapter = new ArrayAdapter<String>(...);
adapter.notifyDataSetChanged();
回答5:
Thanks to everyone who looked at my problem. I hope this solution helps other people.
I was using an adapter for a separated list, which manages adapters for the different sections of the list. The adapter I was having problem with, was one of the managed adapters.
What I did was make a callback function (in Java) that handles the situation when the SeekBar is scrolled. I registered it with my adapter, so when it's called, it calls 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();
}
}
Thanks :)
来源:https://stackoverflow.com/questions/13427069/calling-notifydatasetchanged-from-inside-adapter-fails