当我尝试在 listview 中更新画廊适配器时(我使用回收),有时适配器没有更新,并且视图仍在使用旧适配器。其他所有内容都已更新。为什么?
public View getView(int position, View convertView, ViewGroup parent) {
final int myPosition = position;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (position == title.size() - 1) {
rowView = inflater.inflate(R.layout.emptyview, parent, false);
return rowView;
}
switch (typeOfCell.get(position)) {
case 0:
if (convertView == null) {
rowView = inflater.inflate(R.layout.newstextcelladapter,
parent, false);
} else {
rowView = convertView;
}
break;
case 1:
if (convertView == null) {
rowView = inflater.inflate(R.layout.newsimagecelladapter,
parent, false);
} else {
rowView = convertView;
}
Gallery gallery = (Gallery) rowView
.findViewById(R.id.newsImageGallery);
if (gallery == null) {
Log.e("Gallery is null", "!!!!!!!!!!!!!!!!!!!!");
rowView = inflater.inflate(R.layout.newsimagecelladapter,
parent, false);
}
gallery = (Gallery) rowView.findViewById(R.id.newsImageGallery);
if (gallery == null) {
Log.e("Gallery is still null", "!!!!!!!!!!!!!!!!!!!!");
}
try {
gallery.setAdapter(this.adaptersForGallery.get(position));
if (gallery.getAdapter().hashCode() == this.adaptersForGallery
.get(position).hashCode()) {
Log.e("The gallery adapter is not set", "");
}
Log.w("URL used",
""
+ this.adaptersForGallery.get(position).mImageIds
.get(0));
gallery.setHorizontalFadingEdgeEnabled(false);
gallery.setVisibility(View.GONE);
gallery.setVisibility(View.VISIBLE);
} catch (Exception e) {
@SuppressWarnings("unused")
int a = 1;
Log.w("FATAL", "ERROR");
}
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View view2 = new View(MainActivity.context);
DetailsImageNews.setView(
MainActivity.dataBase
.getCommentsForNews(MainActivity.existingForNews
.get(myPosition)),
MainActivity.dataBase
.getAuthorForNews(MainActivity.existingForNews
.get(myPosition)),
MainActivity.dataBase
.getDateForNews(MainActivity.existingForNews
.get(myPosition)),
MainActivity.dataBase
.getTimeForNews(MainActivity.existingForNews
.get(myPosition)),
MainActivity.dataBase
.getDetailedTextForNews(MainActivity.existingForNews
.get(myPosition)),
MainActivity.dataBase
.getTitleForNews(MainActivity.existingForNews
.get(myPosition)),
MainActivity.detailImageNews, adaptersForGallery
.get(myPosition), view2);
MainActivity.detailsGallery.setSelection(myPosition);
while (MainActivity.currentViewPosition != 1) {
MainActivity.mContainer.showNext();
Log.w("position", "" + MainActivity.currentViewPosition);
if (MainActivity.currentViewPosition > 2) {
MainActivity.currentViewPosition = 0;
}
MainActivity.currentViewPosition += 1;
}
}
});
gallery.refreshDrawableState();
gallery.postInvalidate();
gallery.invalidate();
gallery.refreshDrawableState();
gallery.setVisibility(View.INVISIBLE);
gallery.setVisibility(View.VISIBLE);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
View view2 = new View(MainActivity.context);
DetailsImageNews.setView(
MainActivity.dataBase
.getCommentsForNews(MainActivity.existingForNews
.get(myPosition)),
MainActivity.dataBase
.getAuthorForNews(MainActivity.existingForNews
.get(myPosition)),
MainActivity.dataBase
.getDateForNews(MainActivity.existingForNews
.get(myPosition)),
MainActivity.dataBase
.getTimeForNews(MainActivity.existingForNews
.get(myPosition)),
MainActivity.dataBase
.getDetailedTextForNews(MainActivity.existingForNews
.get(myPosition)),
MainActivity.dataBase
.getTitleForNews(MainActivity.existingForNews
.get(myPosition)),
MainActivity.detailImageNews, adaptersForGallery
.get(myPosition), view2);
MainActivity.detailsGallery.setSelection(myPosition);
while (MainActivity.currentViewPosition != 1) {
MainActivity.mContainer.showNext();
Log.w("position", "" + MainActivity.currentViewPosition);
if (MainActivity.currentViewPosition > 2) {
MainActivity.currentViewPosition = 0;
}
MainActivity.currentViewPosition += 1;
}
}
});
break;
case 2:
if (convertView == null) {
rowView = inflater.inflate(R.layout.newsvideocelladapter,
parent, false);
} else {
rowView = convertView;
}
Uri uri = Uri.parse(videoUrls.get(position));
VideoView video = (VideoView) rowView.findViewById(R.id.newsVideo);
video.setVideoURI(uri);
break;
default:
if (convertView == null) {
rowView = inflater.inflate(R.layout.newstextcelladapter,
parent, false);
} else {
rowView = convertView;
}
break;
}
TextView titleView = (TextView) rowView
.findViewById(R.id.newsTitleView);
TextView dateView = (TextView) rowView.findViewById(R.id.newsDateView);
TextView timeView = (TextView) rowView.findViewById(R.id.newsTimeView);
TextView commentsView = (TextView) rowView
.findViewById(R.id.newsCommentView);
titleView.setText(this.title.get(position));
dateView.setText(this.date.get(position));
timeView.setText(this.time.get(position));
commentsView.setText(Integer.toString(this.comments.get(position)));
return rowView;
}
这种情况会发生十分之一。我曾尝试使用无效,但图像会永远加载。(我从网上下载图像。)。更新:我从回收视图中检查了画廊:画廊没有适配器。怎么办?