Android - ExpandableListView - 在getChildView()中加载图像

时间:2013-10-21 18:14:19

标签: android android-layout android-listview android-asynctask

我对“getChildView”方法的调用量有疑问。

在我的getChildView()方法中,我为每个孩子从服务器加载一个图像并将其放入ImageView。

“加载图像”的业务逻辑是在单独的AsyncTask中编写的。

我的问题是,调用方法“getChildView”的次数超过了孩子的数量。

这非常糟糕,因为调用AsyncTask通常会调用该方法...所以我遇到了性能问题。

也许我的方法并不是真的很好......如果有人能告诉我一个更好的解决方案,不能保持主线程,那就太好了。 :)

PS:“boBilder”类加载图像并将其放入imageview中。

这是我的代码:

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) {
        }
    }

0 个答案:

没有答案