Reputation: 86
i want create a book in android stodio for example: i want when i click on first_season button open text1.txt file and when click on second_season button open text2.txt file and the rest as I said. and i dont want make a activity for each page. and i want use switch case for get button id and based on that id open the text file. i just want to know how use switch case for create this application. i use this code for open txt file:
TextView txtView = (TextView)findViewById(R.id.hellotxt);
InputStream inputStream = getResources().openRawResource(R.raw.nitish);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
txtView.setText(byteArrayOutputStream.toString());
consider names of my buttons are season1, season2, season3, season4 and name of my text files are txt1, txt2, txt3, txt4 thank for helping
Upvotes: 0
Views: 232
Reputation: 76458
Something like this you mean?
Map<Int, String> lookupTable = new HashMap<>();
lookupTable.put(R.id.season1, "txt1");
lookupTable.put(R.id.season2, "txt2");
// etc
for (Map.Entry<String,String> entry : lookupTable.entrySet()) {
findViewById(entry.getKey()).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtView.setText(readBookPageFromFile(entry.getValue());
}
})
}
Where readBookPageFromFile
is your above code with the filename as a parameter.
Upvotes: 1