Hi fellow developers,

I am going bald with this, ripping my hairs.
I spend hours searching google, anddev, stack overflow and forums to
no extent, I have found nothing.

I'm designing a simple app, for my first, that get datas from a web
service.
The main ui launch a thread that will act as a worker, which will send
messages to the ui to give indication to the user what is going on.

At the moment the app is launched, the worker thread sends a
PAGE_LOADING message, which triggers a progressDialog display.
On the PAGE_LOADED message, the progressDialog is dissmissed.
I see that progressDialog.

Now, if I ask the thread to load another page, I see the PAGE_LOADING
message coming to the ui thread, but no progressDialog is displayed
anymore.
Same thing if I trigger another action that should display the
progressDialog...

Can anyone explain me what is wrong with my code?

ui code:
public class MainActivity extends Activity implements ShakerCallback {
    final int              ID_CONFIG    = 10;
    final int              ID_REFRESH   = 20;
    private WatchKeys      keys         = null;
    private KivaWorker     kivaWrkr     = null;
    private ProgressDialog pd;

    public final Handler   mHandlerKiva = new Handler() {
                                            @Override
                                            public void
handleMessage(Message msg) {
                                                KivaMessage kMsg;
                                                KivaLoan kLoan;
                                                switch (msg.what) {
                                                    case
KivaMessage.MSG_LOAN:
                                                        kLoan =
(KivaLoan) msg.obj;
 
Log.d("kiva_thread", "loan received");
 
Log.d("kiva_thread", kLoan.toString());
 
inProgress("Loading...");
 
compose(kLoan);
 
doneProgress();
                                                        break;
                                                    case
KivaMessage.MSG_PAGE_LOADING:
                                                        kMsg =
(KivaMessage) msg.obj;
 
Log.d("kiva_thread", kMsg.msg);
 
inProgress(kMsg.msg);
                                                        break;
                                                    case
KivaMessage.MSG_PAGE_LOADED:
                                                        kMsg =
(KivaMessage) msg.obj;
 
Log.d("kiva_thread", kMsg.msg);
 
doneProgress();
                                                        break;
                                                    default:
                                                        kMsg =
(KivaMessage) msg.obj;
 
Log.d("kiva_thread", kMsg.msg);
                                                        break;
                                                }
                                            }
                                        };

    private void inProgress(String msg) {
        Log.i("pg", "show progress");

        //pd = new
ProgressDialog(this.getApplicationContext());        //Crash on
pd.show() ???
        pd = new ProgressDialog(this);
        pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pd.setIndeterminate(true);
        pd.setTitle("Processing");
        pd.setMessage(msg);
        pd.show();

    }

    /**
     * Dismiss a previously opened ProgressDialog
     */
    private void doneProgress() {
        pd.dismiss();
        pd=null;
        Log.i("pg", "hide progress");
    }


Worker thread:
public class KivaWorker extends Thread {
    private final Handler mHandler;
    final static int      MSG_REFRESH = 10, MSG_LOAN_LOADED = 20;
    private final boolean threadRun   = true;
    public int           page        = 1;
    public int            loanIdx     = 0;
    final static String   URL_LOAN    = "http://api.kivaws.org/v1/
loans/newest.json";
    private Context       ctx         = null;
    public List<KivaLoan> loans;

    public KivaWorker(Handler mHandler, Context ctx) {
        this.mHandler = mHandler;
        this.ctx = ctx;
        this.start();
    }

    /**
     * Worker function. Update with an up to date datas about
available loans from Kiva.
     *
     * @param url
     */
    public void getLatestLoans(String url, int page) {
        Message msg = new Message();
        KivaMessage kMsg;
        kMsg = new KivaMessage("Loading page " + this.page);
        msg.what = KivaMessage.MSG_PAGE_LOADING;
        msg.obj = kMsg;
        this.mHandler.sendMessage(msg);
        if (url == null) {
            url = KivaWorker.URL_LOAN;
        }
        url = url + "?page=" + page;
        final HttpClient httpclient = new DefaultHttpClient();
        String result = "";
        JSONObject json = new JSONObject();
        // Prepare a request object
        final HttpGet httpget = new HttpGet(url);
        final ResponseHandler<String> handler = new
BasicResponseHandler();
        // you result will be String :
        try {
            result = httpclient.execute(httpget, handler);
        }
        catch (final ClientProtocolException e) {
            e.printStackTrace();
        }
        catch (final IOException e) {
            e.printStackTrace();
        }
        try {
            json = new JSONObject(result);
        }
        catch (final JSONException e) {
            e.printStackTrace();
        }
        try {
            final JSONArray loans = json.getJSONArray("loans");
            final JSONObject paging = json.getJSONObject("paging");
            this.page = paging.getInt("page");
            final List<KivaLoan> aryLoans = new ArrayList<KivaLoan>();
            for (int i = 0; i < loans.length(); i++) {
                aryLoans.add(new KivaLoan(loans.getJSONObject(i),
this.ctx, this.mHandler));
            }
            this.loans = aryLoans;
        }
        catch (final JSONException e) {
            e.printStackTrace();
        }
        msg = new Message();
        kMsg = new KivaMessage("Finished loading page " + this.page);
        msg.what = KivaMessage.MSG_PAGE_LOADED;
        msg.obj = kMsg;
        this.mHandler.sendMessage(msg);
        // Alerts.showAlert("", this);
    }

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to