1

我在我的 Android 应用程序中遇到问题。问题是a和 aEdittext没有被改变。没有共享参考,它工作正常。这是我的代码:TextWatcherSharedPreferences

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

它在这里工作:

在这里工作正常

但它不在这里:

在这里不起作用

顺便说一句,edittextdollars是顶部编辑文本,edittexteuros是底部编辑文本,spinner1是顶部微调器,spinner2是底部微调器。

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

任何有关此问题的帮助将不胜感激。

4

0 回答 0