「getChildView」メソッドの呼び出し量に問題があります。
私の getChildView() メソッドでは、子ごとにサーバーから画像をロードし、それを ImageView に入れます。
「イメージの読み込み」のビジネス ロジックは、別の AsyncTask で記述されます。
私の問題は、メソッド「getChildView」が子の数よりも多く呼び出されることです。
メソッドが呼び出されるたびに AsyncTask が呼び出されるため、これは非常に悪いことです...パフォーマンスの問題があります。
たぶん私のアプローチはあまり良くないでしょう...メインスレッドを保持しないというより良い解決策を誰かが教えてくれたら、それは素晴らしいことです。:)
PS: クラス「boBilder」は画像をロードし、画像ビューに配置します。
これが私のコードです:
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.zeile_custom, null);
Log.d("pictures", "--- > getChildView");
final TextView title = (TextView) vi.findViewById(R.id.txtMeldungTitel);
final TextView erstelltAm = (TextView) vi.findViewById(R.id.txtEstelltAm);
final ImageView imgNachricht = (ImageView) vi.findViewById(R.id.imgView);
ProgressBar progess = (ProgressBar) vi
.findViewById(R.id.progressBar_Zeile);
boBilder boBilder = new boBilder();
TextView txtdatum_Erstellung = (TextView) vi
.findViewById(R.id.txtNachricht_Datum);
HashMap<String, String> nachricht = new HashMap<String, String>();
nachricht = this.getChild(groupPosition, childPosition);
// // Wenn noch nicht gelesen, dann anpassen
if (nachricht.get("gelesen").equalsIgnoreCase("J")) {
vi.setBackgroundColor(Color.parseColor("#eeeeee"));
} else {
vi.setBackgroundColor(Color.parseColor("#ffffff"));
title.setTypeface(null, Typeface.BOLD_ITALIC);
erstelltAm.setTypeface(null, Typeface.BOLD_ITALIC);
}
title.setText(nachricht.get("titel"));
erstelltAm.setText(nachricht.get("bezeichnung"));
txtdatum_Erstellung.setText(nachricht.get("datum_erstellung")
.substring(0, 10));
if (!nachricht.get("bild1").equalsIgnoreCase("")) {
boBilder.pruefeBildVorhanden_Liste(nachricht.get("bild1"),
true, 1, this.activity, imgNachricht, progess, this,
groupPosition, childPosition);
}
return vi;
}
「boBilder」のコードは次のとおりです。
public boolean pruefeBildVorhanden_Liste(String nameBild,
boolean herunterladenWennFehlt, int nummerBild, Activity activity,
ImageView imgView, ProgressBar progress,
ListenAdapter listenAdapter, Integer group, Integer child) {
AsyncTaskFTPConnection ftpConnection = new AsyncTaskFTPConnection();
ftpConnection.setActivity(activity);
ftpConnection.setImageView(imgView);
ftpConnection.setAdapter(listenAdapter);
ftpConnection.setProgressBar(progress);
ftpConnection.setSuchBild(nameBild, nummerBild);
ftpConnection.groupPosition = group;
ftpConnection.childPosition = child;
ftpConnection.execute("");
return true;
}
public class AsyncTaskFTPConnection extends AsyncTask<String, Void, String> {
public DatenTransferFTPServer ftpConn;
private Nachricht_Detail activity = null;
private Activity act = null;
private ImageView imgView = null;
private ProgressBar progress = null;
public Integer groupPosition;
public Integer childPosition;
private ListenAdapter adapter = null;
private String cSuchBild;
private int nummerBild;
public void setActivity(Nachricht_Detail fenster) {
if (fenster != null) {
this.activity = fenster;
}
}
public void setActivity(Activity fenster) {
if (fenster != null) {
this.act = fenster;
}
}
public void setImageView(ImageView imgView) {
this.imgView = imgView;
}
public void setProgressBar(ProgressBar progress) {
this.progress = progress;
}
public void setAdapter(ListenAdapter adapter) {
this.adapter = adapter;
}
public void setSuchBild(String cName, int nummerBild) {
this.cSuchBild = cName;
this.nummerBild = nummerBild;
}
@Override
protected String doInBackground(String... params) {
if (this.activity != null) {
this.ftpConn = new DatenTransferFTPServer("", "", "",
this.activity);
} else {
this.ftpConn = new DatenTransferFTPServer("", "", "", this.act);
}
String returnString = "";
File bild = null;
if (this.activity != null) {
bild = new File(Pfade.gebenPfad(this.activity,
Pfade.PfadeSammlung.LOKAL_BILDER_ORDNER)
+ this.cSuchBild);
} else {
bild = new File(Pfade.gebenPfad(this.act,
Pfade.PfadeSammlung.LOKAL_BILDER_ORDNER)
+ this.cSuchBild);
}
if (!bild.exists()) {
if (this.ftpConn.StatusVerbindung) {
// Picture is not downloaded yet ..
if (this.activity != null) {
if (this.ftpConn.downloadDatei(
Pfade.gebenPfad(this.activity,
Pfade.PfadeSammlung.FTP_BILDER_ORDNER)
+ this.cSuchBild, bild.getPath())) {
returnString = "erfolg";
}
} else {
if (this.ftpConn.downloadDatei(
Pfade.gebenPfad(this.act,
Pfade.PfadeSammlung.FTP_BILDER_ORDNER)
+ this.cSuchBild, bild.getPath())) {
returnString = "erfolg";
}
}
} else {
// No connection
returnString = "keineVerbindung";
}
} else {
// Picture is already downloaded ...
returnString = "erfolg";
}
return returnString;
}
@Override
protected void onPostExecute(String result) {
if (result.equalsIgnoreCase("erfolg")) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap;
try {
if (this.activity != null) {
bitmap = BitmapFactory
.decodeStream(
new FileInputStream(
Pfade.gebenPfad(
this.activity,
Pfade.PfadeSammlung.LOKAL_BILDER_ORDNER)
+ this.cSuchBild),
null, options);
((Nachricht_Detail) this.activity).zeigeBild(bitmap,
this.nummerBild);
} else {
bitmap = BitmapFactory
.decodeStream(
new FileInputStream(
Pfade.gebenPfad(
this.act,
Pfade.PfadeSammlung.LOKAL_BILDER_ORDNER)
+ this.cSuchBild),
null, options);
this.imgView.setImageBitmap(bitmap);
Log.d("pictures", "Set pic!");
this.progress.setVisibility(ProgressBar.INVISIBLE);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
this.imgView.setImageResource(R.drawable.icon_nachricht);
this.progress.setVisibility(ProgressBar.INVISIBLE);
}
} else {
// Error ... set default pic
this.imgView.setImageResource(R.drawable.icon_nachricht);
this.progress.setVisibility(ProgressBar.INVISIBLE);
}
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}