我在android.developer.com上阅读了关于“使ListView滚动平滑”的文章,但有一点我不明白:放置或获取“位置”的值,因为我的eclipse说:“不能参考到在不同方法中定义的内部类中的非最终变量位置“。 我把这段代码放在getView方法的适配器类中。
package com.example.filemanager;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Formatter.BigDecimalLayoutForm;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.DateSorter;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ItemAdapter extends ArrayAdapter<Item> {
private final Context context;
private final ArrayList<Item> values;
public int position;
public ItemAdapter(Context context, ArrayList<Item> values) {
super(context, R.layout.item, values);
this.context = context;
this.values = values;
}
@Override
public Item getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item, parent, false);
TextView title = (TextView) rowView.findViewById(R.id.title);
TextView info = (TextView) rowView.findViewById(R.id.info);
ImageView icon = (ImageView) rowView.findViewById(R.id.icon);
ImageView menu = (ImageView) rowView.findViewById(R.id.menu);
title.setText(values.get(position).getName());
info.setText(sizeString(position) + " " + dateString(position) + " "
+ rightsString(position));
if (values.get(position).isisDir()) {
icon.setImageResource(R.drawable.dir);
} else if (!values.get(position).isisDir()) {
if (values.get(position).getPath().endsWith(".jpg")
|| values.get(position).getPath().endsWith(".png")) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 50;
options.inDensity = 100;
options.inScreenDensity = 1;
Bitmap myBitmap = BitmapFactory.decodeFile(values.get(position).getPath(), options);
icon.setImageBitmap(myBitmap);
}else{
icon.setImageResource(R.drawable.file);
}
}
menu.setImageResource(R.drawable.menu);
ViewHolder holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.menu = (ImageView) convertView.findViewById(R.id.menu);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.info = (TextView) convertView.findViewById(R.id.info);
holder.progress = (ProgressBar) convertView.findViewById(R.id.progressSpinner);
convertView.setTag(holder);
// Using an AsyncTask to load the slow images in a background thread
new AsyncTask<ViewHolder, Void, Bitmap>() {
private ViewHolder v;
@Override
protected Bitmap doInBackground(ViewHolder... params) {
v = params[0];
//return mFakeImageLoader.getImage();
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (v.position == position) {
// If this item hasn't been recycled already, hide the
// progress and set and show the image
v.progress.setVisibility(View.GONE);
v.icon.setVisibility(View.VISIBLE);
v.icon.setImageBitmap(result);
}
}
}.execute(holder);
return rowView;
}
private String rightsString(int position) {
String temp = "";
if (values.get(position).isReadable()) {
temp += "r";
} else {
temp += "-";
}
if (values.get(position).isWritable()) {
temp += "w";
} else {
temp += "-";
}
if (values.get(position).isExecutable()) {
temp += "x";
} else {
temp += "-";
}
return temp;
}
private String dateString(int position) {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
return df.format(values.get(position).getDate());
}
private String sizeString(int position) {
long bytes = values.get(position).getSize();
double kb = bytes / 1024.0;
double mb = kb / 1024.0;
double gb = mb / 1024.0;
if (gb >= 1) {
return gb + " GB";
}
if (mb >= 1) {
return mb + " MB";
}
if (kb >= 1) {
return kb + " KB";
} else {
return bytes + " bytes";
}
}
static class ViewHolder {
ImageView icon, menu;
TextView title, info;
ProgressBar progress;
int position;
}
}
答案 0 :(得分:0)
好的,所以你需要了解一下外部,内部和匿名类等。发生的事情是你正在创建一个带有
声明的匿名类new AsyncTask()....
并且该类内部的任何变量都必须是final或不引用父类。
所以,你需要做的是:
final int finalPosition = position; // Using an AsyncTask to load the slow images in a background thread new AsyncTask() { private ViewHolder v; @Override protected Bitmap doInBackground(ViewHolder... params) { v = params[0]; return null; } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); if (v.position == finalPosition) { // If this item hasn't been recycled already, hide the // progress and set and show the image v.progress.setVisibility(View.GONE); v.icon.setVisibility(View.VISIBLE); v.icon.setImageBitmap(result); } } }.execute(holder);