在对此投反对票之前请多多包涵。我是 Android 的初学者。在此处尝试多种解决方案后,我未能解决此问题。
我正在尝试使用以下项目在列表视图中设置图像。我希望能够在计划适配器类的某处插入图像代码。
以下代码是我的计划适配器
public class Schedule_ArrayAdapter extends ArrayAdapter<String> {
private final Activity activity;
private final String[] name, category, image;
Typeface colab, colab_bold, Bebas;
int selected = -1;
public Schedule_ArrayAdapter(Activity activity, String[] name, String[] category, String[] image) {
super(activity, R.layout.schedule_item, category);
this.activity = activity;
this.name = name;
this.category = category;
this.image = image;
this.colab = Typeface.createFromAsset(activity.getAssets(), "ColabThi.otf");
this.colab_bold = Typeface.createFromAsset(activity.getAssets(), "ColabMed.otf");
this.colab_bold = Typeface.createFromAsset(activity.getAssets(), "BebasNeue.otf");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//get view and textview from xml
View rowView = inflater.inflate(R.layout.schedule_item, parent, false);
rowView.setBackgroundColor(0xffffffff);
LinearLayout background = (LinearLayout) rowView.findViewById(R.id.back);
if (selected != -1) {
if (selected == position) {
background.setBackgroundColor(0xffeaac4b);
} else {
background.setBackgroundColor(0xffffffff);
}
}else{
background.setBackgroundColor(0xffffffff);
}
TextView TimeView = (TextView) rowView.findViewById(R.id.category);
TextView TitleView = (TextView) rowView.findViewById(R.id.title);
ImageView vi = (ImageView) rowView.findViewById(R.id.imgPreview);
GetImage getimage = new GetImage();
getimage.execute(image);
//change names
TitleView.setText(category[position]);
TitleView.setTextSize(fontpercent_screenheight(3.5));
TitleView.setPadding(dp(10), dp(5), dp(5), dp(0));
TitleView.setTypeface(Bebas);
TimeView.setText(name[position]);
TimeView.setTextSize(fontpercent_screenheight(3.5));
TimeView.setPadding(dp(10), dp(2), dp(5), dp(5));
TimeView.setTypeface(colab);
return rowView;
}
以下是我的 ImageDownloader 类
public class ImageDownloader {
private InputStream OpenHttpConnection(String image) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(image);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
conn.setUseCaches(true);
HttpURLConnection httpConn = (HttpURLConnection) conn;
try {
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting" + ex);
}
return in;
}
public Bitmap DownloadImage(String image) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(image);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
}
解析 JSON 和获取数据正常工作。在调试器中查看时,“图像”数组也被获取。如何在视图中设置图像?