これに反対票を投じる前に、ご容赦ください。私はAndroidの初心者です。ここで複数のソリューションを試した後、これを解決できませんでした。
次の項目を配置して、リストビューにイメージを設定しようとしています。Schedule アダプタ クラスのどこかにイメージ コードを挿入できるようにしたいと考えています。
次のコードは私のスケジュールアダプターです
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 の解析とデータのフェッチは正常に機能しています。デバッガーを見ると、「イメージ」配列もフェッチされています。ビューに画像を設定するにはどうすればよいですか?