ScrollView в тексте с URL - PullRequest
       11

ScrollView в тексте с URL

0 голосов
/ 03 апреля 2012

Итак, я пытаюсь показать этот список клана в своем приложении с этим кодом в упражнении

        /* 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); 
    }
}

Но как мне включить прокрутку, потому что она не использует макет, который я сделал: Roster.xml?Итак, как мне заставить его работать, чтобы я мог прокручивать до имен, которые находятся еще ниже в списке?

1 Ответ

0 голосов
/ 03 апреля 2012

Вы можете взять представление прокрутки как

    ScrollView sv = new ScrollView(this);

, затем добавить свой TextView к этому ScrollView

    sv.addView(tv);

, а затем установить это представление прокрутки как ContentView как

    this.setContentView(sv);

введите свой код следующим образом:

//     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); 
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...