Visualizzazione stampabile
-
no forse non ci stiamo capendo
private String inputStreamToString(InputStream helpfile) {
// TODO Auto-generated method stub
return null;
}
questa funzione dovrebbe ritornare la stringa con cui vuoi valorizzare la textview. guarda prova a far ritornare una stringa fissa, tipo return "testo di prova"
-
Se inserisco return "testo di prova" funzia.
-
e certo.... in quella funzione devi tu scrivere il codice che ti estrae il testo dall'inputstream, ho fatto una rapida ricerca su google e ho trovato il seguente codice, vedi se lo puoi adattare alle tue esigenze
codice:
public String convertStreamToString(InputStream is)
throws IOException {
/*
* To convert the InputStream to String we use the
* Reader.read(char[] buffer) method. We iterate until the
* Reader return -1 which means there's no more data to
* read. We use the StringWriter class to produce the string.
*/
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(
new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
return writer.toString();
} else {
return "";
}
}
-
Ok, teoricamente l'ho capito. Sto provando ad implementarlo ma ho delle difficoltà, in particolare con i metodi autogenerati try/catch.
Ho dovuto implementarli xkè mi dava un errore (e quindi il consiglio try/catch)sul metodo inputStreamToString.
Spero di non aver scritto un mucchio di stronzate!roftl
-
Si effettivamente nel codice che avevo postato manca il blocco catch. Implementa sia il catch che il finally. Era questo il dubbio?
-
ancora niente. Pagina totalmente vuota.
-
Appena torno a casa stasera faccio qualche prova e ti faccio sapere
-
Quote:
Originariamente inviato da
Anular
ancora niente. Pagina totalmente vuota.
ciao scusami ma non ho potuto ancora avuto tempo di provare il codice, però ad occhio e croce ad uno sguardo veloce....
penso dovresti cambiare
codice:
helpstring = inputStreamToString(helpfile);
in
codice:
helpText.setText(inputStreamToString(helpfile));
e
in
codice:
return (writer.toString());
-
nada. sta diventando un'impresa :D
-
allora... ho testato velocemente il seguente codice e funziona tutto, confrontalo con il tuo
codice:
package myTest.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class main extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
InputStream helpfile = getResources().openRawResource(R.raw.indirizzi);
TextView helpText = (TextView) findViewById(R.id.TextView01);
try
{
helpText.setText(inputStreamToString(helpfile));
}
catch (IOException e)
{
e.printStackTrace();
}
}
private String inputStreamToString(InputStream helpfile) throws IOException
{
if (helpfile != null)
{
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try
{
Reader reader = new BufferedReader(new InputStreamReader(helpfile, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1)
{
writer.write(buffer, 0, n);
System.out.println("ciao");
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
helpfile.close();
}
return writer.toString();
}
else
{
return null;
}
}
}