输入第一个值后,TextWatcher和SharedPreferences无法识别Edittext

时间:2013-10-13 15:56:23

标签: java android sharedpreferences textwatcher

我在Android应用程序中遇到问题。问题是,EdittextTextWatcher未更改SharedPreferences。它的工作效果很好,没有共享的参考。这是我的代码:

public interface CurrencyConverter {
    public double convert(String currencyFrom, String currencyTo)
            throws Exception;
}

public class YahooCurrencyConverter implements CurrencyConverter {
    public double convert(String currencyFrom, String currencyTo)
            throws IOException {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(
                "http://quote.yahoo.com/d/quotes.csv?s=" + currencyFrom
                        + currencyTo + "=X&f=l1&e=.csv");
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpGet, responseHandler);
        httpclient.getConnectionManager().shutdown();
        return Double.parseDouble(responseBody);
    }
}
public String convertvalues(String convertfrom, String convertto) {
    double current;
    double val = Double.parseDouble(edittextdollars.getText()
            .toString());
    DecimalFormat df = new DecimalFormat(".##");
    YahooCurrencyConverter ycc = new YahooCurrencyConverter();
    try {
        current = ycc.convert(convertfrom, convertto);
        edittexteuros.setText(df.format(val * current));
        return "passed";
    } catch (Exception e) {

        return "passed";
    }
}

private void addListenerOnButton2() {
    edittextdollars.addTextChangedListener(new TextWatcher() {
           public void afterTextChanged(Editable s) {
           }

           public void beforeTextChanged(CharSequence s, int start, 
             int count, int after) {

           }
public void onTextChanged(CharSequence s, int start, 
             int before, int count) {
                String text1 = spinner1.getSelectedItem().toString().trim();
                String text2 = spinner2.getSelectedItem().toString().trim();
           //TextView myOutputBox = (TextView) findViewById(R.id.myOutputBox);
           //myOutputBox.setText(s);
                String hello = edittextdollars.getText().toString();
                if (hello.matches("")) {
                    img1.setImageDrawable(null);
                    edittexteuros.setText("");
                }
                if (edittextdollars.getText().toString().length() != 0) {                          
                if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR")) {
                    try {
                       convertvalues("USD", "EUR");
                       getGraph("USD", "EUR");
                       String hello1 = edittextdollars.getText().toString();
                        double hello2 = Double.parseDouble(hello1);
                        String hello3 = edittexteuros.getText().toString();
                        double hello4 = Double.parseDouble(hello3);
                        String h = Double.toString(hello4 / hello2);
                        SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences("rohit", 0);
                        SharedPreferences.Editor editor = sharedPrefs.edit();
                        editor.putString(getString(R.string.data1), h);
                        editor.commit();
                    } catch(Exception e) {
                        SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences("rohit", 0);
                        String name2 = sharedPrefs.getString(getString(R.string.data1), "not found");
                        double name3 = Double.parseDouble(name2);
                        String hello1 = edittextdollars.getText().toString();
                        double hello2 = Double.parseDouble(hello1);
                        double ee = hello2 * name3;
                        //String h = Double.toString(ee);
                        //edittexteuros.setText(h);
                        DecimalFormat df = new DecimalFormat(".##");
                        edittexteuros.setText(df.format(hello2 * name3));
                    }
               }

它在这里工作:

Works fine here http://i41.tinypic.com/2s7fm1l.png

但它不在这里:

Doesn't work here http://i41.tinypic.com/dzwjg2.png

顺便说一下,edittextdollars是顶级编辑文字,edittexteuros是底层编辑文字,spinner1是顶级微调框,spinner2是底部微调框。

这里有什么问题?我遇到了什么问题?我使用共享偏好的原因是互联网连接断开时。正如我所说,它与互联网连接完美无瑕。那么我在SharedPreferences中添加了哪些错误?

对于这个问题,我们将不胜感激。

0 个答案:

没有答案