I have a TextView
that displays different text files depending on the user selection from a Spinner
. How can I get the name of the text file that is being displayed? The files are in the raw folder.
Asked
Active
Viewed 247 times
0

David Medenjak
- 33,993
- 14
- 106
- 134

vin shaba
- 167
- 3
- 12
-
How do you display your text? If you read the text from the file, then you obviously have access to the file. Hence you can read the filename. – David Medenjak Feb 12 '16 at 17:47
-
yea i have the first file name but i need to get other names of files being selected – vin shaba Feb 12 '16 at 17:49
-
Care to show us some of your code? Also check `getSelectedItem()` – Pedro Oliveira Feb 12 '16 at 17:55
-
the code i have is for reading the file, havent come up with any for getting file name. just help me out, u have a textview, how do u get the name of the text file being displayed. thanx – vin shaba Feb 12 '16 at 17:59
-
check this link http://stackoverflow.com/a/7486969/4848308 – Gueorgui Obregon Feb 12 '16 at 17:59
1 Answers
0
This is the way to obtain the file from raw after being selected in a spinner
Spinner mySpinner=(Spinner) findViewById(R.id.your_spinner);
String rawTextName = = mySpinner.getSelectedItem().toString();
InputStream ins = getResources().openRawResource(
getResources().getIdentifier(rawTextName ,
"raw", getPackageName()));
If you need to read the content this should work
TextView textView =(TextView ) findViewById(R.id.your_textview);
textView.setText = readTextFile(ins);
...
public String readTextFile(InputStream inputStream) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
} catch (IOException e) {
}
return outputStream.toString();
}
Hope this helps!!

Gueorgui Obregon
- 5,077
- 3
- 33
- 57
-
thanx but all i want is if u have a TextView, how do u get the name of the text file being displayed, forget the spinner. – vin shaba Feb 12 '16 at 18:30
-