I've got some troubles with notifyDataSetChanged() of a BaseAdapter. This method is called in refreshItems() and shall update the BaseAdapter of my ListActivity. On calling notifyDataSetChanged() nothing happens until I scroll down the ListView for example with the arrow keys. Somehow the modified getView() method also is not called. Maybe you can give me a hint - thanks! :)
public class WinampControlClientPlaylist extends ListActivity {
static WinampControlClientPlaylist activity = null;
static EfficientAdapter adapter = null;
static class EfficientAdapter extends BaseAdapter {
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
private LayoutInflater mInflater;
@Override
public int getCount() {
return Settings.playlistlength;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.listview, null);
holder.text = (TextView) convertView.findViewById(R.string.playlist_title);
holder.image = (ImageView) convertView.findViewById(R.string.playlist_play);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(Settings.playlist[position]);
if (position == Settings.playlistPosition)
{
holder.text.setTypeface(null, Typeface.ITALIC);
holder.image.setVisibility(0);
}
else
{
holder.text.setTypeface(null, Typeface.NORMAL);
holder.image.setVisibility(4);
}
return convertView;
}
static class ViewHolder {
TextView text;
ImageView image;
}
@Override
public Object getItem(int position) {
return Settings.playlist[position];
}
}
void initialize()
{
adapter = new EfficientAdapter(this);
setListAdapter(adapter);
//registerForContextMenu(getListView());
}
@Override
public void onResume()
{
super.onResume();
// REFRESH PLAYLIST
if (getListAdapter() == null && Settings.playlist != null)
initialize();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
activity = this;
}
static void refreshItems()
{
try {
adapter.notifyDataSetChanged();
} catch (Exception e) {}
}
}
I had the same problem (ListView updates only when i scroll it, even notifyDataSetChanged didn't help). i solve it this way: just try to do your "view modifications" in thread which creates your user interface i.e.
activity.runOnUiThread(new Runnable() {
public void run() {
//do your modifications here
// for example
adapter.add(new Object());
adapter.notifyDataSetChanged()
}
});
Try calling invalidate()
on your ListView.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With