2012-04-03 5 views
0

Also ich versuche, diesen Clan Roster mit diesem Code in meiner Anwendung zu zeigen, in der TätigkeitScroll in Text von URL

 /* We will show the data we read in a TextView. */ 
     TextView tv = new TextView(this); 

     /* Will be filled and displayed later. */ 
     String myString = null; 
     try { 
      /* Define the URL we want to load data from. */ 
      //http://androidtest.host.org/roster.txt 
      URL myURL = new URL( 
         "http://androidtest.host.org/roster.txt"); 
      /* Open a connection to that URL. */ 
      URLConnection ucon = myURL.openConnection(); 

      /* Define InputStreams to read 
       * from the URLConnection. */ 
      InputStream is = ucon.getInputStream(); 
      BufferedInputStream bis = new BufferedInputStream(is); 

      /* Read bytes to the Buffer until 
       * there is nothing more to read(-1). */ 
      ByteArrayBuffer baf = new ByteArrayBuffer(50); 
      int current = 0; 
      while((current = bis.read()) != -1){ 
        baf.append((byte)current); 
      } 

      /* Convert the Bytes read to a String. */ 
      myString = new String(baf.toByteArray()); 
     } catch (Exception e) { 
      /* On any Error we want to display it. */ 
      myString = e.getMessage(); 
     } 
     /* Show the String on the GUI. */ 
     tv.setText(myString); 
     this.setContentView(tv); 
    } 
} 

Aber wie Scrollen i aktivieren, da es nicht das Layout wird mit i gemacht: Roster .xml? Also wie bekomme ich es funktioniert, so dass ich zu den Namen scrollen kann, die noch weiter unten auf der Liste sind?

Antwort

0

können Sie eine Scrollview wie

nehmen
ScrollView sv = new ScrollView(this); 

dann Textview zu diesem Scroll

sv.addView(tv); 

dann fügen Sie diesen Scroll als InhaltAlle gesetzt wie

this.setContentView(sv); 

setzen Sie Ihren Code wie folgt :

//  take here a scrollview /////////////////////////////////////// 
    ScrollView sv = new ScrollView(this); 

    /* We will show the data we read in a TextView. */ 
    TextView tv = new TextView(this); 

    /* Will be filled and displayed later. */ 
    String myString = null; 
    try { 
     /* Define the URL we want to load data from. */ 
     //http://androidtest.host.org/roster.txt 
     URL myURL = new URL( 
        "http://androidtest.host.org/roster.txt"); 
     /* Open a connection to that URL. */ 
     URLConnection ucon = myURL.openConnection(); 

     /* Define InputStreams to read 
      * from the URLConnection. */ 
     InputStream is = ucon.getInputStream(); 
     BufferedInputStream bis = new BufferedInputStream(is); 

     /* Read bytes to the Buffer until 
      * there is nothing more to read(-1). */ 
     ByteArrayBuffer baf = new ByteArrayBuffer(50); 
     int current = 0; 
     while((current = bis.read()) != -1){ 
       baf.append((byte)current); 
     } 

     /* Convert the Bytes read to a String. */ 
     myString = new String(baf.toByteArray()); 
    } catch (Exception e) { 
     /* On any Error we want to display it. */ 
     myString = e.getMessage(); 
    } 
    /* Show the String on the GUI. */ 
    tv.setText(myString); 
// add your textview to scrollview ///////////////////////////////////// 
     sv.addView(tv); 

// NOW set scrollview as your contentview ///////////////////////////// 
     this.setContentView(sv); 
    } 
} 
+0

Könnten Sie bitte meinen Code korrigieren? Ich kann wirklich nicht herausfinden, wie man den Code verwendet, den du gepostet hast :) Und gibt es eine Möglichkeit, Hintergrundfarbe zu setzen? – SnoX

+0

Ich habe scrollview zu Ihrem Code hinzugefügt; dann fügte sie der scrollview textview hinzu und ordnete diese scrollview als contentview ein. Jus versuchen Sie diesen Code, ich denke, dass es meistens funktioniert. –

+0

Bitte sagen Sie mir, wo ich den Code setzen soll, ich verstehe nicht. Und kannst du eine bgcolor einstellen? – SnoX