我对“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) {
}
}