Search code examples
androidarraylisttextviewsettext

android set text to arraylist of textviews


How to set text to an array of TextViews.

This is what I made:

public void onClickButtons(View view) {

    if(view==mBtnGuess) {
        int i;
        String word = "someword";
        String getInput = mEtxtUserInput.getText().toString();
        char[] wordChars = word.toCharArray();
        if(getInput.length()>0) {

            if(getInput.length()==1) {
                List<TextView> txtCharArr= new ArrayList<TextView>();
                txtCharArr.add(mChar1);
                txtCharArr.add(mChar2);
                txtCharArr.add(mChar3);
                txtCharArr.add(mChar4);
                txtCharArr.add(mChar5);
                txtCharArr.add(mChar6);
                txtCharArr.add(mChar7);
                txtCharArr.add(mChar8);
                txtCharArr.add(mChar9);
                txtCharArr.add(mChar10);
                txtCharArr.add(mChar11);
                txtCharArr.add(mChar12);
                StringBuilder sb = new StringBuilder();
                for(i=0;i<wordChars.length;i++) {
                    if(wordChars[i]==(word.charAt(i))) {
                        txtCharArr.get(i).setText(word.charAt(i));
                        sb.append(word.charAt(i));
                    }
                }
            }
        }
    }
}

The stringbuilder is just to save all chars that where equal. the above code gives an error:

03-14 07:44:27.387: E/AndroidRuntime(1893): FATAL EXCEPTION: main
03-14 07:44:27.387: E/AndroidRuntime(1893): java.lang.IllegalStateException: Could not     execute method of the activity
03-14 07:44:27.387: E/AndroidRuntime(1893):     at android.view.View$1.onClick(View.java:3591)
03-14 07:44:27.387: E/AndroidRuntime(1893):     at android.view.View.performClick(View.java:4084)
03-14 07:44:27.387: E/AndroidRuntime(1893):     at android.view.View$PerformClick.run(View.java:16966)
03-14 07:44:27.387: E/AndroidRuntime(1893):     at android.os.Handler.handleCallback(Handler.java:615)
03-14 07:44:27.387: E/AndroidRuntime(1893):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 07:44:27.387: E/AndroidRuntime(1893):     at android.os.Looper.loop(Looper.java:137)
03-14 07:44:27.387: E/AndroidRuntime(1893):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-14 07:44:27.387: E/AndroidRuntime(1893):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 07:44:27.387: E/AndroidRuntime(1893):     at java.lang.reflect.Method.invoke(Method.java:511)
03-14 07:44:27.387: E/AndroidRuntime(1893):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-14 07:44:27.387: E/AndroidRuntime(1893):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-14 07:44:27.387: E/AndroidRuntime(1893):     at dalvik.system.NativeStart.main(Native Method)
03-14 07:44:27.387: E/AndroidRuntime(1893): Caused by: java.lang.reflect.InvocationTargetException
03-14 07:44:27.387: E/AndroidRuntime(1893):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 07:44:27.387: E/AndroidRuntime(1893):     at java.lang.reflect.Method.invoke(Method.java:511)
03-14 07:44:27.387: E/AndroidRuntime(1893):     at android.view.View$1.onClick(View.java:3586)
03-14 07:44:27.387: E/AndroidRuntime(1893):     ... 11 more
03-14 07:44:27.387: E/AndroidRuntime(1893): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x76
03-14 07:44:27.387: E/AndroidRuntime(1893):     at android.content.res.Resources.getText(Resources.java:229)
03-14 07:44:27.387: E/AndroidRuntime(1893):     at android.widget.TextView.setText(TextView.java:3620)
03-14 07:44:27.387: E/AndroidRuntime(1893):     at  me.mikey.my.games.galgjex.Galgje.onClickButtons(Galgje.java:232)

line 232 is:

txtCharArr.get(i).setText(word.charAt(i));

I also tried:

public void onClickButtons(View view) {

        if(view==mBtnGuess) {
            int i;
            String word = "someword";
            String getInput = mEtxtUserInput.getText().toString();
            if(getInput.length()>0) {

                if(getInput.length()==1) {
                    List<TextView> txtCharArr= new ArrayList<TextView>();
                    txtCharArr.add(mChar1);
                    txtCharArr.add(mChar2);
                    txtCharArr.add(mChar3);
                    txtCharArr.add(mChar4);
                    txtCharArr.add(mChar5);
                    txtCharArr.add(mChar6);
                    txtCharArr.add(mChar7);
                    txtCharArr.add(mChar8);
                    txtCharArr.add(mChar9);
                    txtCharArr.add(mChar10);
                    txtCharArr.add(mChar11);
                    txtCharArr.add(mChar12);
                    StringBuilder sb = new StringBuilder();
                    for(i=0;i<getInput.length();i++) {
                        if(getInput.equals(word.charAt(i))) {
                            txtCharArr.get(i).setText(word.charAt(i));
                            sb.append(word.charAt(i));
                        }
                    }
                }
            }
        }
    }

But this doesn't show anything. the StringBuilder is empty and there are no characters shown in the textViews when I input a character that should match.


Solution

  • Because there is no TextView.setText(char x), your char is cast to an int, and TextView.setText(int x) is called, which searches for the resource with id x.

    You should pass a String to the setText() method. You can try

    txtCharArr.get(i).setText(Character.toString(word.charAt(i)));

    or

    txtCharArr.get(i).setText(word.subString(i, i+1)));