Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call setText() using a Float data type?

Tags:

android

    public void onClick(View v) {
    switch(v.getId()){
    case R.id.save2:
        s = text.getText().toString();
        number = Float.parseFloat(num.getText().toString());
        SharedPreferences.Editor editor = shared.edit();
        editor.putString("sharedtext",s);
        editor.putFloat("sharednum",number);
        editor.commit();
        Toast.makeText(this,"save2",Toast.LENGTH_SHORT).show();
        break;
    case R.id.load2:
        String returner = shared.getString("sharedtext","Returner fail");
        float returnnum = shared.getFloat("sharednum",0);
        text.setText(returner);
        num.setText(returnnum); //error here
        Toast.makeText(this,"load2",Toast.LENGTH_SHORT).show();
        break;
    case R.id.page2:
        intent= new Intent(this,SharedPreferencesActivity.class);
        startActivity(intent);
        Toast.makeText(this,"page2",Toast.LENGTH_SHORT).show();
        break;
    }

how could i resolve this error?

how to make something like setInt(int num);

btw the variable num and text are both EditText

like image 329
Imp Pale Avatar asked Jan 18 '12 09:01

Imp Pale


1 Answers

If you pass a numeric value as the text to a text field, Android will try to interpret it as a resource Id. Make it a text first. The preferred way is to do:

num.setText(String.valueOf(returnnum));

(For good practices on conversion to string, check this post)

like image 105
Guillaume Avatar answered Oct 04 '22 04:10

Guillaume