简体   繁体   中英

Setting TextView to Array value

I'm trying to make a small app that takes an input in an EditText number and takes the corresponding value from an array and displays it in a TextView

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText e = (EditText) findViewById(R.id.editText1);
    final TextView t = (TextView) findViewById(R.id.textView1);
    Button b = (Button) findViewById(R.id.button1);

    Resources res = this.getResources();

    String arr[] = getResources().getStringArray(R.array.example);

    final Editable input = e.getText();

    final String in2 = input.toString();
    int number = Integer.parseInt(in2);

    if(number < 0){
        t.setText("Input is too small");
    } else if (number > 666){
        t.setText("Input is too large");
    } else {
        final String out = arr[number].toString();
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                t.setText(in2);
            }
        }); 
    } 
}

I want it so that if the input is 0 and the button is clicked the TextView is A.

<string-array name="example">
    <item >A</item>
    <item >B</item>
    <item >C</item>
    <item >D</item>
</string-array>

Unfortunately whenever I start this up on an emulator or a phone it instantly crashes. Does anyone know what I'm doing wrong?

The problem is that when you start your app, your editText is empty.

So in2 equals to "" , and hence you can't parse this value to an Integer.

Move it into your onClick method:

b.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {                
        int number = Integer.parseInt(e.getText().toString());
        if(number < 0){
            t.setText("Input is too small");
        } else if (number >= arr.length){
            t.setText("Input is too large");
        } else {
            t.setText(arr[number]);
        }
    }); 
} 

Okay just by looking at the code, I don't think it will accomplish what you are trying to achieve!

final Editable input = e.getText();

final String in2 = input.toString();
int number = Integer.parseInt(in2);

This happens in the onCreate method. So when the app runs, it immediately tries to extract some text, but there's nothing to extract so Integer.parseInt(in2) will throw a runtime exception.

This is my suggested workflow, which may involve some re-writing of your code.

In the onCreate method:

  1. Get all the objects you need via findViewById as you have done.

  2. Set listeners to the editText field and the button - the listener for the editText is responsible for tracking any changes that the user types in. In the methods given by the listener, include the following code:

    stringFromEditText = editText.getText().toString();

    Of course you should have declared these earlier on to make that work:

    String stringFromEditText;

    EditText editText;

After that, you can just call an updateTextView() method (which you define on your own). You don't even need a button! Everything is done within the listener's methods (eg onTextChanged )

In the updateTextView() method, don't forget to put some logic in that deals with bad inputs or null inputs: when you encounter bad inputs, simply set the value of stringFromEditText to "0". So you would always succeed in parsing it in later -> no more crashes!

I know I haven't been too detailed by providing all the code. But I hope this helps.

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