简体   繁体   中英

SetText String[] in a TextView

I am trying to use setText , and I want to use a String array. First, I create a String [] , then I assign data to String[0] , then I want to .setText(String[0]) on my TextView , is this the right way? Note : I'm using a StringTokenizer to split Strings in the textfile

  try {
    filename = "myk.txt";
    FileReader filereader = new FileReader(Environment.getExternalStorageDirectory() +      "/Q/" + filename);
    BufferedReader bufferedreader = new BufferedReader(filereader);


      try {
        while ((text = bufferedreader.readLine()) != null){
            sb.append(text);
            sb.toString().split(";");
            tokens = new StringTokenizer(sb.toString(), ";");

            ///NULLPOINTER EXEPTION HERE//// if (tokens.countTokens() > 0){questionfromfile[0] = tokens.nextToken();
            }


        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
 ////ETC ...//// and now textview.setText(question[0]);

Sure, you mean something like

String[] strings = new String [5];
strings[0] = "foobar";
component.setText(strings[0]);

why do you have this line:

sb.toString().split(";");

?

are you forgetting that a string is immutable ,meaning that using the standard API that you use , the string will never change itself , but create new objects instead?

about StringTokenizer, as javadocs say:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

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