简体   繁体   中英

Setting a TextView to the value of an integer

public class DataHelper extends Activity{

    TextView tvRslt;

    public static int insert(String firstFB, String firstRL) {
        // TODO Auto-generated method stub
        Integer a = new Integer(firstFB).intValue();
        Integer b = new Integer(firstRL).intValue();
        int firstResult = a / b;
        }

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

I wanted to set the integer "firstResult" to display on the TextEdit "tvRslt".

How do I do this?

If any further information is required, let me know.

With exception handling:

try {
    tvRslt.setText(firstResult + "");
} catch (DivideByZeroException e) {
    // Do something with exception condition.
}

Without exception handling:

// Before dividing check if denominator is 0.
int firstResult = (b == 0) ? 0 : a / b;
tvRslt.setText(firstResult + "");

This does of course require that you have a reference to the textview, as I assume you have in the code you did not show us.

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