我有一个适配器(扩展 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()" );
}
任何见解表示赞赏!
谢谢你。