1

我制作了一个 Android 应用程序,其中我在 5 个不同的活动中从用户那里接收文本和数字(十进制)值,然后将它们存储在我的共享首选项中。我可以使用 editor(); 恢复所有值;在之前的所有 5 个活动中。

现在

在最终计算活动中,我正在恢复所有用户输入(文本和数字)并将它们存储在字符串和双精度变量中。在下面发布的代码活动中执行计算;

问题

我无法使用 getText 在 textview 中查看结果。

有什么遗漏吗?

public class Xcalculateforall extends Activity {

    Button qcbut, coatsolbut;
    TextView TVqcbut, TVcoatsolbut, TVbrand;
    double tdrum, rqctotal;
    String a;
    public static String FILE1 = "MyPrefsFile";
    SharedPreferences abcPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resultmain);

        qcbut = (Button) findViewById(R.id.bQCS);
        coatsolbut = (Button) findViewById(R.id.bCSP);

        TVqcbut = (TextView) findViewById(R.id.tvQCS);
        TVbrand = (TextView) findViewById(R.id.tvBrand);
        TVcoatsolbut = (TextView) findViewById(R.id.tvCSP);

        abcPref = this.getSharedPreferences(FILE1, 0);

        a = abcPref.getString("tA", ""); //receives text value and store in 'a' String

        double k = Integer.parseInt(abcPref.getString("tsK", ""));
        double l = Integer.parseInt(abcPref.getString("tsL", ""));
        double m = Integer.parseInt(abcPref.getString("tsM", ""));
        double n = Integer.parseInt(abcPref.getString("tsN", ""));
        double o = Integer.parseInt(abcPref.getString("tsO", ""));
        double p = Integer.parseInt(abcPref.getString("tsP", ""));
        tdrum = 20 / m;
        double samv = (tdrum / k) / l;
        double samv2 = (Math.sqrt(samv));
        double tsample = ((samv2 + 1) * k) * l;
        double rmicro = tsample * n;
        double rchem = tsample * o;
        double rother = tsample * p;
        double rinqc = rmicro + rchem + rother;
        rqctotal = rinqc - 2;

        qcbut.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                TVqcbut.setText(rqctotal + "Days");
                TVbrand.setText("" + a);
                // Intent results = new Intent("com.traviss.calculate.RESULT");
                // startActivity(results);
            }
        });

    }
}

更新查询 --- 我接受用户输入的先前活动

public class G2J extends Activity {
    Button two2five, save2;

    EditText edtG, edtH, edtI, edtJ, edtK;
    int tG, tH, tI, tJ, tK;
    String tsG, tsH, tsI, tsJ, tsK;
    public static String FileP2 = "MyPrefsFile";
    SharedPreferences abcPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.g2j);
        two2five = (Button) findViewById(R.id.btp2);

        edtG = (EditText) findViewById(R.id.etG);
        edtH = (EditText) findViewById(R.id.etH);
        edtI = (EditText) findViewById(R.id.etI);
        edtJ = (EditText) findViewById(R.id.etJ);
        edtK = (EditText) findViewById(R.id.etK);

        abcPref = getSharedPreferences(FileP2, 0);
        edtG.setText(abcPref.getString("tsG", ""));
        edtH.setText(abcPref.getString("tsH", ""));
        edtI.setText(abcPref.getString("tsI", ""));
        edtJ.setText(abcPref.getString("tsJ", ""));
        edtK.setText(abcPref.getString("tsK", ""));

        two2five.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if ((!edtG.getText().toString().equals(""))
                        && (!edtH.getText().toString().equals(""))
                        && (!edtI.getText().toString().equals(""))
                        && (!edtJ.getText().toString().equals(""))
                        && (!edtK.getText().toString().equals(""))) {
                    abcPref = G2J.this.getSharedPreferences(FileP2, 0);
                    SharedPreferences.Editor editor = abcPref.edit();
                    editor.putString("tsG", edtG.getText().toString());
                    editor.putString("tsH", edtH.getText().toString());
                    editor.putString("tsI", edtI.getText().toString());
                    editor.putString("tsJ", edtJ.getText().toString());
                    editor.putString("tsK", edtK.getText().toString());
                    editor.commit();
                    Toast message = Toast.makeText(G2J.this, "Values are saved", 2000);
                    message.setGravity(Gravity.BOTTOM, 0, 0);
                    message.show();
                    Intent openl2p = new Intent("com.traviss.calculate.L2P");
                    startActivity(openl2p);
                }
                else {
                    Toast failz = Toast.makeText(G2J.this,
                            "Values are not Entered", 2000);
                    failz.setGravity(Gravity.BOTTOM, 0, 0);
                    failz.show();
                }
            };
        });

    }
}
4

3 回答 3

0

检查您的字符串变量和 xml 布局

于 2013-09-16T07:12:08.980 回答
0

尝试 -

int k = Integer.parseInt(abcPref.getString("tsK", ""));
于 2013-09-15T06:08:30.017 回答
0

您不会使用 Integer.parseInt(String)获得十进制值

做这个

 double k = Double.parseDouble(abcPref.getString("tsK", ""));

  a = String.valueOf(abcPref.getString("tA", ""));
于 2013-09-15T06:25:36.797 回答