0

I'm trying to display simple text I retrieve from a URL. This code works fine if I run it using System.out.println() to the java console, but when I change to Android, no text is displayed. Since I can get text to display if I hard code a string in setText, I assume my problem is in ArrayList. Thank you in advance for your help.

onCreate

try {

    ArrayList<String> myData = new ArrayList<String>();
    LinearLayout lView = new LinearLayout(this);
    TextView myText = new TextView(this);

    BufferedReader br = null;       
    URL url;

    url = new URL("URL to plain text");
    br = new BufferedReader(new InputStreamReader(url.openStream()));
    String inputLine;

    while ((inputLine = br.readLine()) != null) {
        myData.add(inputLine);
    }

    br.close();

    myText.setText(myData.get(1));
    lView.addView(myText);
    setContentView(lView);


} catch (IOException e) {
    e.printStackTrace();
}       
6
  • Try setting the text after you added it to the view. Commented Apr 25, 2014 at 16:06
  • I hope this is prototype code, otherwise please seperate at least UI and file IO. Commented Apr 25, 2014 at 16:07
  • myText.setText(myData.get(1)); can't do that on a background thread. Commented Apr 25, 2014 at 16:08
  • and you know for a fact that myData.get(1) contains something because you logged it? Commented Apr 25, 2014 at 16:12
  • Probably there is a NetworkOnMainThreadException? Commented Apr 25, 2014 at 16:14

1 Answer 1

1

Do it that way:

myText.setText(Arrays.toString(myData.toArray(new String[myData.size()])));

It'll print the whole contents of myData.

Sign up to request clarification or add additional context in comments.

3 Comments

not relevant to the question. the question is not the get the content of myData, as logging to sysout apparently works.
@njzk2 I can't say, if myData.get(1) is not an empty String. So it might help to solve the problem.
thanks for that comment Denis. It is relevant to the question that I have. Ironically @njzk2 made a snarky comment on my question as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.