简体   繁体   中英

Edittext nor ProgressDialog doesn't appear

I am encountering a problem where my progressdialog doesn't show or do anything. I am making a currency converter that converts values. Here is my code:

if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            while (edittextdollars.equals("")) {
                final ProgressDialog myPd_ring=ProgressDialog.show(getActivity(), "", "Loading...", true);
                myPd_ring.setCancelable(true);
                new Thread(new Runnable() {  
                      @Override
                      public void run() {
                            try
                            {
                                convertvalues("USD", "EUR");        
                            }catch(Exception e){

                            }
                            myPd_ring.dismiss();
                      }
                }).start();
            }
        }

Without the progressdialog, like shows:

    if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {

                convertvalues("USD", "EUR");

}

It works, but the "Calculate" button is pressed down for a few seconds before the value returns. I want a progressdialog to show that the value is loading. I have searched Google, but nothing worked. Any help regarding this problem is appreciated.

PS I would prefer you post code instead of just saying a solution without any code. Also, please post any websites that you would think is helpful for me (like tutorials). Thanks once again.

EDIT:

When I added this code:

if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            myPd_ring = ProgressDialog.show(getActivity(), "", "Loading...", true, true);
            new Thread(new Runnable() {  
                @Override
                public void run() {
                    try {
                        convertvalues("USD", "EUR");        
                    } catch(Exception e) {

                    }

                }
            }).start();             
        }        

@Override
public void afterTextChanged(Editable arg0) {
    myPd_ring.dismiss();
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

}

I have implemented TextWatcher, but it doesn't work. The progressdialog stays there forever and the value doesn't change. Any help would be greatly appreciated.

What you have done is not correct.

while (edittextdollars.equals("")) { // this while loop is not the way to wait for calculation finish...inside this while loop you are spwaning a thread which is not correct
    final ProgressDialog myPd_ring=ProgressDialog.show(getActivity(), "", "Loading...", true);  //here you are showing the dialog
    myPd_ring.setCancelable(true);
    new Thread(new Runnable() { 
        @Override
        public void run() {
            try {
                convertvalues("USD", "EUR");        
            } catch(Exception e) {

            }
            myPd_ring.dismiss(); // the run method can be called within few milliseconds of starting the thread..so in essence immediately you are removing your dialog 
        }
    }).start(); // just after showing the dialog you are starting the thread
}

I would suggest the following changes (I am assuming that your method convertvalues is the one which changes the text of edittexteuros ):

1. Declare myPd_ring in your activity class

ProgressDialog myPd_ring = null; 

2. Let your activity implement TextWatcher (add the unimplemented methods)

public class YourActivity extends Activity implements TextWatcher

3. Add TextWatcher with your EditText edittexteuros .

edittexteuros = (EditText) findViewById(R.id.youreditTextId);
edittexteuros.addTextChangedListener(YourActivity.this);

4. The calculation Part and showing the Dialog

if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
    myPd_ring = ProgressDialog.show(getActivity(), "", "Loading...", true, true);
    new Thread(new Runnable() {  
        @Override
        public void run() {
            try {
                convertvalues("USD", "EUR");        
            } catch(Exception e) {

            }

        }
    }).start();
}

5. The dismissal of Dialog from afterTextChanged

@Override
public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub
    myPd_ring.dismiss();
}

EDIT:

Are you sure that current is always a valid value? As a precaution you need to show some error text in case of exception (you always need to set some text else the dialog will never close). Please debug and check if you are always getting a proper response from the network or not. The code which I have given to you works perfectly in my test setup, so your problem might be lying somewhere else now. You will need to debug and find that out.

YahooCurrencyConverter ycc = new YahooCurrencyConverter();                  
try {
    current = ycc.convert(convertfrom, convertto);
    edittexteuros.setText(df.format(val*current)); 
    return "passed";
} catch (Exception e) {
    edittexteuros.setText("some error message"); 
    return "passed";
}
if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
             showprogress();
                convertvalues("USD", "EUR");
             dissmissprogress();
}

showprogress(){
 dialog = new ProgressDialog(youractivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show()
}
dissmissprogress(){
dialog.dismiss();
}

Hope this is what you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM