当我使用带有适配器的listview,在视图中添加一个库并且图库适配器加载图像时,图库会加载带有asynctask的图像,图库在不同的地方显示。
public View getView(int position, View convertView, ViewGroup parent) {
final int myPosition = position;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
boolean isConverted = false;
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;
isConverted = true;
}
Gallery gallery = (Gallery) rowView.findViewById(R.id.newsImageGallery);
try{
gallery.setAdapter(this.adaptersForGallery.get(position));
Log.w("Adapter used", "" + this.adaptersForGallery.get(position));
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");
}
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);
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;
}
public View getView(int position, View convertView, ViewGroup parent) {
View i ;
ImageView iv;
ProgressBar mProgressBar;
calledGetView+=1;
Log.i("Image View:URL", ""+mImageIds.get(position));
if(convertView == null)
{
i = MainActivity.factory.inflate(R.layout.imagespinner, null);
}
else
{
i = (ImageView) convertView;
}
mProgressBar =(ProgressBar) i.findViewById(R.id.imageProgress);
iv = (ImageView) i.findViewById(R.id.image);
try {
AsycTask task = new AsycTask();
task.url = new URL(mImageIds.get(position));
task.iv = iv;
task.execute(iv);
iv.setAdjustViewBounds(true);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new RelativeLayout.LayoutParams((int) (MainActivity.screenWidth*0.7), (int) ((MainActivity.screenWidth*0.7)*0.6)));
task.globalPosition = currentPosition;
task.mProgressBar = mProgressBar;
if(task.bm == null)
{
}
}
catch (IOException e) {
Log.i("Asyc task", "FATAL ERROR");
e.printStackTrace();
}
return i;
}
public class AsycTask extends AsyncTask<ImageView, Void, Bitmap> {
public Bitmap bm;
public Bitmap bm2;
public int globalPosition;
public ProgressBar mProgressBar;
public int imagePosition;
public ImageView iv;
public URL url;
public Canvas c;
@Override
protected Bitmap doInBackground(ImageView... arg0) {
iv = arg0[0];
//Log.w("Loading url:",""+ url.getPath());
try {
bm2 = BitmapFactory.decodeStream((InputStream) url.getContent());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OutOfMemoryError e) {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inSampleSize = 2;
o.inDither = false;
o.inPurgeable = true;
try {
bm2 = BitmapFactory.decodeStream(
(InputStream) url.getContent(), null, o);
} catch (IOException e1) {
e1.printStackTrace();
} catch (OutOfMemoryError e1) {
}
}
return bm2;
}
private void updateView(int index){
View v = MainActivity.newsListView.getChildAt(index -
MainActivity.newsListView.getFirstVisiblePosition());
}
@Override
protected void onPostExecute(Bitmap result) {
mProgressBar.setVisibility(View.GONE);
iv.setVisibility(View.GONE);
iv.setVisibility(View.VISIBLE);
BitmapDrawable dr = ((BitmapDrawable) iv.getDrawable());
if (dr != null) {
Bitmap bmForRecyl = dr.getBitmap();
if (bmForRecyl != null) {
bmForRecyl.recycle();
}
}
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setImageBitmap(result);
Log.i("Set", "setted");
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
mProgressBar.incrementProgressBy(5);
}
} 我知道这个问题,但我不知道如何解决它与带有asyctask的库的listview。 我想这是因为图库没有加载新的适配器。如何更改适配器?
答案 0 :(得分:0)
您需要覆盖getViewItemType
。
getViewTypeCount()
- 此方法返回信息列表中有多少行类型
getItemViewType(int position)
- 根据位置
然后,只有在布局为空时才对其进行膨胀,并使用getItemViewType
确定类型。
示例:
private static final int TYPE_ITEM1 = 0;
private static final int TYPE_ITEM2 = 1;
private static final int TYPE_ITEM3 = 2;
@Override
public int getItemViewType(int position)
{
int type;
if (position== 0){ // your condition
type = TYPE_ITEM1; //type 0 for image
} else if(position == 1){
type = TYPE_ITEM2; //type 1 for text
}else {
type = TYPE_ITEM3; //type 2 for videos
}
return type;
}
@Override
public int getViewTypeCount() {
return 3; //three different layouts to be inflated
}
在getView
int type= getItemViewType(arg0);
switch (type) {
case TYPE_ITEM1:
// inflate layout for text
break;
case TYPE_ITEM2:
// inflate layout for image
break;
case TYPE_ITEM3:
// inflate layout for video
break;
....
您可以查看以下教程