gpt4 book ai didi

java - 无法在自定义适配器中引用异步任务

转载 作者:行者123 更新时间:2023-12-01 13:35:44 26 4
gpt4 key购买 nike

问题是,当我在自定义适配器 getView 函数中设置异步下载任务时,它返回

ImageLoader cannot be resolved to a type

错误。该类位于同一项目的不同包中,通常可以导入,但在这种情况下它只显示错误。如何解决这个问题?感谢您的帮助

LeadersAdapter.java

public class LeadersAdapter extends ArrayAdapter<Record> {


public LeadersAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}

public LeadersAdapter(Context context, int resource, List<Record> items) {
super(context, resource, items);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View v = convertView;

if (v == null) {

LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.leader_record_row, null);

}

Record p = getItem(position);

if (p != null) {

ImageView pic = (ImageView) v.findViewById(R.id.profile_pic);
TextView name = (TextView) v.findViewById(R.id.name);
TextView pts = (TextView) v.findViewById(R.id.pts);

new ImageLoader().execute(pic,p.url);
name.setText(p.name);
pts.setText(p.pts);
}

return v;

}
}

ImageLoader.java

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

class ImageLoader extends AsyncTask<Object, Void, Bitmap> {

private static String TAG = "ImageLoader";
public InputStream input;
public ImageView view;
public String imageURL;

public ImageLoader(String text){

}

@Override
protected Bitmap doInBackground(Object... params) {
try {

view = (ImageView) params[0];
imageURL = (String) params[1];

URL url = new URL(imageURL);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

@Override
protected void onPostExecute(Bitmap result) {
if (result != null && view != null) {
view.setImageBitmap(result);
}
}
}

最佳答案

您的ImageLoader类(未定义为public)正在使用默认访问修饰符package-private,因此它只是在它自己的包中可见。

改变...

类 ImageLoader [...]

到...

公共(public)类 ImageLoader [...]

应该可以解决您的可访问性问题。

此外,我强烈建议您研究现有的图像加载库(例如 Universal Image Loader ),而不是自行烘焙。

关于java - 无法在自定义适配器中引用异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21289373/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com