问题
I have this weird problem with position variable inside getView method of the adapter. 4 of those 6 different view types, got a button inside them. That button sends a message to a service (provoking some async stuff on the service), and within an indefinite amount of time after, this adapter gets a notifyDataSetChanged() provoked by the service.
The problem shows when i spam the button that sends the message. If i spam it fast enough, the wrong position will be sent to the service. I think the problem is that during the spam, i will hit the button during a notifyDataSetChanged(), because if i comment that call on the methods that the service is using, this inconsistency wont happen.
This is the first time i use BaseAdapter, and i followed this nice tutorial: Base Adapter Tutorial
Below are the code parts that i think can be relevant to pinpoint the issue.
Theres 6 different view types being managed by this adapter:
private static final int MAX_COUNT = 6;
And here are the methods im overriding:
@Override
public int getViewTypeCount() {
return MAX_COUNT;
}
@Override
public int getCount() {
return data.size();
}
@Override
public ListViewDataItem getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return getItem(position).getType();
}
And heres the getView method:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Thread.currentThread().setContextClassLoader(MyParcelableFile.class.getClassLoader());
View row = convertView;
StandardFolderViewHolder standardFolderViewHolder = null;
StandardFileViewHolder standardFileViewHolder = null;
MusicFileStoppedViewHolder musicFileStoppedHolder = null;
MusicFilePlayingViewHolder musicFilePlayingHolder = null;
MusicFolderStoppedViewHolder musicFolderStoppedHolder = null;
MusicFolderPlayingViewHolder musicFolderPlayingHolder = null;
switch(getItemViewType(position)) {
case Constants.MEDIA_FILE.TYPE.STANDARD_DIRECTORY:
if(row == null) {
standardFolderViewHolder = new StandardFolderViewHolder();
row = inflater.inflate(R.layout.listview_mixed_folder_row, parent, false);
standardFolderViewHolder.icon = (ImageView)row.findViewById(R.id.filetype_icon);
standardFolderViewHolder.tempTV = (TextView)row.findViewById(R.id.listview_mixed_folder_row_test_tv);
row.setTag(standardFolderViewHolder);
}
else {
standardFolderViewHolder = (StandardFolderViewHolder)row.getTag();
}
standardFolderViewHolder.icon.setImageDrawable(getItem(position).getDrawable());
standardFolderViewHolder.tempTV.setText(getItem(position).getName());
standardFolderViewHolder.tempTV.setSelected(true);
break;
case Constants.MEDIA_FILE.TYPE.MUSIC_DIRECTORY:
if(getItemViewState(position) == Constants.MEDIA_FILE.TYPE.STATE.PLAYING) {
if(row == null || (row !=null && row.getTag() instanceof MusicFolderStoppedViewHolder)) {
musicFolderPlayingHolder = new MusicFolderPlayingViewHolder();
row = inflater.inflate(R.layout.listview_music_folder_playing_row, parent, false);
musicFolderPlayingHolder.icon = (ImageView)row.findViewById(R.id.filetype_icon);
musicFolderPlayingHolder.songName = (TextView)row.findViewById(R.id.row_title_tv);
musicFolderPlayingHolder.playButton = (Button)row.findViewById(R.id.row_play_button);
musicFolderPlayingHolder.durationTV = (TextView)row.findViewById(R.id.row_duration_tv);
musicFolderPlayingHolder.progressBar = (ProgressBar)row.findViewById(R.id.folder_progress_bar);
row.setTag(musicFolderPlayingHolder);
}
else {
musicFolderPlayingHolder = (MusicFolderPlayingViewHolder)row.getTag();
}
musicFolderPlayingHolder.icon.setImageDrawable(getItem(position).getDrawable());
musicFolderPlayingHolder.songName.setText(getItem(position).getName());
musicFolderPlayingHolder.songName.setSelected(true);
musicFolderPlayingHolder.durationTV.setText(mActivity.formattedMillis(getItem(position).getDuration()));
musicFolderPlayingHolder.progressBar.setMax(getItem(position).getDuration());
musicFolderPlayingHolder.progressBar.setProgress(getItem(position).getProgress());
musicFolderPlayingHolder.playButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.e("clicked", getItem(position).getName());
Bundle bun = new Bundle();
bun.putString(Constants.BUNDLE_KEYS.PLAY_FILES, getItem(position).getPath());
Message message = Message.obtain(null, Constants.OP_CODE.PLAY_FILES);
message.setData(bun);
try {
mActivity.mService.send(message);
}
catch (RemoteException re) {
re.printStackTrace();
}
}
});
}
else if(getItemViewState(position) == Constants.MEDIA_FILE.TYPE.STATE.STOPPED) {
if(row == null || (row !=null && row.getTag() instanceof MusicFolderPlayingViewHolder)) {
musicFolderStoppedHolder = new MusicFolderStoppedViewHolder();
row = inflater.inflate(R.layout.listview_music_folder_stopped_row, parent, false);
musicFolderStoppedHolder.icon = (ImageView)row.findViewById(R.id.filetype_icon);
musicFolderStoppedHolder.songName = (TextView)row.findViewById(R.id.row_title_tv);
musicFolderStoppedHolder.playButton = (Button)row.findViewById(R.id.row_play_button);
musicFolderStoppedHolder.durationTV = (TextView)row.findViewById(R.id.row_duration_tv);
row.setTag(musicFolderStoppedHolder);
}
else {
musicFolderStoppedHolder = (MusicFolderStoppedViewHolder)row.getTag();
}
musicFolderStoppedHolder.icon.setImageDrawable(getItem(position).getDrawable());
musicFolderStoppedHolder.songName.setText(getItem(position).getName());
musicFolderStoppedHolder.songName.setSelected(true);
musicFolderStoppedHolder.durationTV.setText(mActivity.formattedMillis(getItem(position).getDuration()));
musicFolderStoppedHolder.playButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.e("clicked", getItem(position).getName());
Bundle bun = new Bundle();
bun.putString(Constants.BUNDLE_KEYS.PLAY_FILES, getItem(position).getPath());
Message message = Message.obtain(null, Constants.OP_CODE.PLAY_FILES);
message.setData(bun);
try {
mActivity.mService.send(message);
}
catch (RemoteException re) {
re.printStackTrace();
}
}
});
}
break;
case Constants.MEDIA_FILE.TYPE.STANDARD_FILE:
if(row == null) {
standardFileViewHolder = new StandardFileViewHolder();
row = inflater.inflate(R.layout.listview_mixed_folder_row, parent, false);
standardFileViewHolder.icon = (ImageView)row.findViewById(R.id.filetype_icon);
standardFileViewHolder.tempTV = (TextView)row.findViewById(R.id.listview_mixed_folder_row_test_tv);
row.setTag(standardFileViewHolder);
}
else {
standardFileViewHolder = (StandardFileViewHolder)row.getTag();
}
standardFileViewHolder.icon.setImageDrawable(getItem(position).getDrawable());
standardFileViewHolder.tempTV.setText(getItem(position).getName());
standardFileViewHolder.tempTV.setSelected(true);
break;
case Constants.MEDIA_FILE.TYPE.MUSIC_FILE:
if(getItemViewState(position) == Constants.MEDIA_FILE.TYPE.STATE.PLAYING) {
if(row == null || (row !=null && row.getTag() instanceof MusicFileStoppedViewHolder)) {
musicFilePlayingHolder = new MusicFilePlayingViewHolder();
row = inflater.inflate(R.layout.listview_music_file_playing_row, parent, false);
musicFilePlayingHolder.icon = (ImageView)row.findViewById(R.id.filetype_icon);
musicFilePlayingHolder.songName = (TextView)row.findViewById(R.id.row_title_tv);
musicFilePlayingHolder.playButton = (Button)row.findViewById(R.id.row_play_button);
musicFilePlayingHolder.durationTV = (TextView)row.findViewById(R.id.row_duration_tv);
musicFilePlayingHolder.progressBar = (ProgressBar)row.findViewById(R.id.folder_progress_bar);
row.setTag(musicFilePlayingHolder);
}
else {
musicFilePlayingHolder = (MusicFilePlayingViewHolder)row.getTag();
}
musicFilePlayingHolder.icon.setImageDrawable(getItem(position).getDrawable());
musicFilePlayingHolder.songName.setText(getItem(position).getName());
musicFilePlayingHolder.songName.setSelected(true);
musicFilePlayingHolder.durationTV.setText(mActivity.formattedMillis(getItem(position).getDuration()));
musicFilePlayingHolder.progressBar.setMax(getItem(position).getDuration());
musicFilePlayingHolder.progressBar.setProgress(getItem(position).getProgress());
musicFilePlayingHolder.playButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.e("clicked", getItem(position).getName());
Bundle bun = new Bundle();
bun.putString(Constants.BUNDLE_KEYS.PLAY_FILES, getItem(position).getPath());
Message message = Message.obtain(null, Constants.OP_CODE.PLAY_FILES);
message.setData(bun);
try {
mActivity.mService.send(message);
}
catch (RemoteException re) {
re.printStackTrace();
}
}
});
}
else if(getItemViewState(position) == Constants.MEDIA_FILE.TYPE.STATE.STOPPED) {
if(row == null || (row !=null && row.getTag() instanceof MusicFilePlayingViewHolder)) {
musicFileStoppedHolder = new MusicFileStoppedViewHolder();
row = inflater.inflate(R.layout.listview_music_file_stopped_row, parent, false);
musicFileStoppedHolder.icon = (ImageView)row.findViewById(R.id.filetype_icon);
musicFileStoppedHolder.songName = (TextView)row.findViewById(R.id.row_title_tv);
musicFileStoppedHolder.playButton = (Button)row.findViewById(R.id.row_play_button);
musicFileStoppedHolder.durationTV = (TextView)row.findViewById(R.id.row_duration_tv);
row.setTag(musicFileStoppedHolder);
}
else {
musicFileStoppedHolder = (MusicFileStoppedViewHolder)row.getTag();
}
musicFileStoppedHolder.icon.setImageDrawable(getItem(position).getDrawable());
musicFileStoppedHolder.songName.setText(getItem(position).getName());
musicFileStoppedHolder.songName.setSelected(true);
musicFileStoppedHolder.durationTV.setText(mActivity.formattedMillis(getItem(position).getDuration()));
musicFileStoppedHolder.playButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.e("clicked", getItem(position).getName());
Bundle bun = new Bundle();
bun.putString(Constants.BUNDLE_KEYS.PLAY_FILES, getItem(position).getPath());
Message message = Message.obtain(null, Constants.OP_CODE.PLAY_FILES);
message.setData(bun);
try {
mActivity.mService.send(message);
}
catch (RemoteException re) {
re.printStackTrace();
}
}
});
}
break;
}
if(!getItem(position).wasAnimatedIn()) {
row.startAnimation(getItem(position).getGoingIn());
}
else if (!getItem(position).wasAnimatedOut()) {
Animation outAnim = getItem(position).getGoingOut();
outAnim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
data.remove(getItem(position));
}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationStart(Animation animation) {}
});
row.startAnimation(outAnim);
}
return row;
}
One of the methods, on this adapter, that the service may call:
public void activatePlayingState(int positionInPage) {
if(positionInPage < getCount()) {
ListViewDataItem lvDataItem = getItem(positionInPage);
lvDataItem.setState(Constants.MEDIA_FILE.TYPE.STATE.PLAYING);
notifyDataSetChanged();
}
}
回答1:
Positions aren't meant to be stable in the same way ids are. An example of this is choice modes. If your ids aren't stable, any changes in the list (moving/adding/removing items) will upset checked positions since there is really no way short of storing all items to track where each goes. Incidentally though not really related to your issue, if you DO have stable ids and an item moves more than 20 items or so, they just clear the item's checked state. At the time the code was written, I would assume they thought traversing ~20 items to check position v. id was all that could be performed in an efficient enough way.
In your case, while you may not be moving items around yourself, internally items DO move around when you call notifyDataSetChanged()
in a sense. AdapterView.AdapterDataSetObserver#onChanged shows exactly what happens when you call notifyDataSetChanged()
.
To get to the point, you can fix your issue by using stableIds instead of positions. To implement that, change your getItemId(int position)
method to return a unique id for the item at that position. Then, override hasStableIds()
to return true. Here's the docs for hasStableIds()
BaseAdapter#hasStableIds(). Now, you'll be able pass on the id to your service. You're already passing a bundle to your service, so just put the id in that. Also note that you'll need to store the ids for the items with a state you need to track. That's as simple as adding the ids to an ArrayList for example. When your service does whatever it does, it can then call your activatePlayingState
method using the id instead of a possibly stale position (remember to change that parameter from int to long). In getView
, you can then compare the activated id with the current getView item using getItemId(int position)
and setting the views for that item as expected.
来源:https://stackoverflow.com/questions/19547472/baseadapters-getview-getting-wrong-position