0

私はアンドロイドが初めてです。アプリに XML 解析を使用していますが、アプリをクリックすると背景画像とその上に ProgessBar が表示され、データが表示されなくなるまで画面が保持されます。私は自分のアプリをBBCニュースのAndroidアプリのようにしています

AsyncTask<Void, Void, Drawable> downloader = new AsyncTask<Void, Void, Drawable>() {

    @Override
    protected Drawable doInBackground(Void... params) {
        AndroidHttpClient client = AndroidHttpClient.newInstance(context.getPackageName());
        try {
            HttpGet get = new HttpGet(url);
            final HttpParams httpParams = new BasicHttpParams();
            HttpClientParams.setRedirecting(httpParams, true);
            get.setParams(httpParams);
            HttpResponse resp = client.execute(get);
            int status = resp.getStatusLine().getStatusCode();
            if(status != HttpURLConnection.HTTP_OK){
                //Log.i(LOGTAG, "Couldn't download image from Server: " + url + " Reason: " + resp.getStatusLine().getReasonPhrase() + " / " + status);
                return null;
            }
            HttpEntity entity = resp.getEntity();
            //Log.i(LOGTAG, url + " Image Content Length: " + entity.getContentLength());
            InputStream is = entity.getContent();
            FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
            copyStream(is, fos);
            fos.close();
            is.close();
            FileInputStream  fis = context.openFileInput(filename);
            return loadDrawableFromStream(context, fis);
        }
        catch (Exception ex) {
            //Log.e(LOGTAG, "Exception during Image download of " + url, ex);
            return null;
        }
        finally {
            client.close();
        }
    }

    @Override
    protected void onPostExecute(Drawable result) {
        if (result == null)
            result = defaultDrawable;
        mPendingDownloads.remove(url);
        cache.put(url, result);
        for (ImageView iv: downloads) {
            // validate the url it is waiting for
            String pendingUrl = mPendingViews.get(iv);
            if (!url.equals(pendingUrl)) {
                //Log.i(LOGTAG, "Ignoring out of date request to update view for " + url);
                continue;
            }
            mPendingViews.remove(iv);
            if (result != null) {
                final Drawable newImage = result;

                Drawable newSize=resize(newImage);


                final ImageView imageView = iv;

                imageView.setImageDrawable(newSize);
            }
        }
    }

    private BitmapDrawable resize(Drawable newImage) {
        // TODO Auto-generated method stub

        Bitmap d = ((BitmapDrawable)newImage).getBitmap();
        Bitmap bitmapOrig = Bitmap.createScaledBitmap(d, 75, 75, false);
        return new BitmapDrawable(bitmapOrig);                                              
    }
};
downloader.execute();
}

private static Hashtable<ImageView, String> mPendingViews = new Hashtable<ImageView, String>();
private static Hashtable<String, ArrayList<ImageView>> mPendingDownloads = new Hashtable<String, ArrayList<ImageView>>();
}
4

2 に答える 2

0

非同期タスク:

private ProgressDialog mDialog;

Override
protected void onPreExecute() {
     mDialog = new ProgressDialog(...);
     mDialog.setContentView(R.layout.dialog_layout);

     // define your mDialog object further...

     mDialog.show()
}

@Override
protected Drawable doInBackground(...) {
     // do stuff
}

@Override
protected void onPostExecute(...) {
     mDialog.dismiss();
}

dialog_layout.xml (疑似)

<LinearLayout
     background=...>

     <ProgressBar />

</LinearLayout>
于 2012-09-21T12:41:29.647 に答える
0
private ProgressDialog progressDialog;

protected void onPreExecute() {
        progressDialog = ProgressDialog.show(context, "", context.getResources().getString(R.string.searching_wait_message), true);
}
protected void onPostExecute(Vector<SearchResult> result) {
    progressDialog.dismiss();
}
于 2012-09-21T12:07:43.240 に答える