我需要用線程運行我的代碼的一些部分。但是我訪問run()
函數中的變量時遇到問題。變量(也是函數參數)需要被定義爲final,但是當我這樣做時,我不能在run()
函數中更改它們的值。例如,現在變量iv
在run()
方法中不可訪問。在類函數中運行線程
有什麼辦法可以解決這個問題嗎?
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
convertView = getLayoutInflater().inflate(R.layout.gallery_gridsq, parent, false);
ImageView iv = (ImageView) convertView.findViewById(R.id.icon);
final File file = new File(Uri.parse(getItem(position).toString()).getPath());
Runnable runnable = new Runnable() {
@Override
public void run() {
Bitmap bmp = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
BitmapFactory.decodeStream(new FileInputStream(file), null, options);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
options.inJustDecodeBounds = false;
options.inSampleSize = 2;
try {
bmp = BitmapFactory.decodeStream(new FileInputStream(file), null, options);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
iv.setImageBitmap(bmp);
}
};
new Thread(runnable).start();
return convertView;
}
你不重新分配'iv'變量......它可以是'final' –