I want to add text files to tabs for the action bar in Android. A for loop will look at each file. If the file contains content, this will be added as the tab title. The for loop is designed to skip over any files that do not exist. This however is not the case; if a file does not exist, the adding of tabs will not continue.
I know that the files are not corrupted as when i add the text file to make it exist again, all the text files will turn to tabs.
for (addTabPosition = 2; addTabPosition < 11; addTabPosition++) {
GetTopics();
if(addTabMessage.contentEquals("FileNotFound")){
Log.e("skiper", "file skiped" + addTabPosition);
}else{
switch (addTabPosition) {
case 2:
ActionBar.Tab tab2=actionBar.newTab();
tab2.setText(addTabTitle);
tab2.setTabListener(this);
actionBar.addTab(tab2);
tabNumber++;
break;
case 3:
ActionBar.Tab tab3=actionBar.newTab();
tab3.setText(addTabTitle);
tab3.setTabListener(this);
actionBar.addTab(tab3);
tabNumber++;
break;
case 4:
ActionBar.Tab tab4=actionBar.newTab();
tab4.setText(addTabTitle);
tab4.setTabListener(this);
actionBar.addTab(tab4);
tabNumber++;
break;
case 5:
ActionBar.Tab tab5=actionBar.newTab();
tab5.setText(addTabTitle);
tab5.setTabListener(this);
actionBar.addTab(tab5);
tabNumber++;
break;
case 6:
ActionBar.Tab tab6=actionBar.newTab();
tab6.setText(addTabTitle);
tab6.setTabListener(this);
actionBar.addTab(tab6);
tabNumber++;
break;
case 7:
ActionBar.Tab tab7=actionBar.newTab();
tab7.setText(addTabTitle);
tab7.setTabListener(this);
actionBar.addTab(tab7);
tabNumber++;
break;
case 8:
ActionBar.Tab tab8=actionBar.newTab();
tab8.setText(addTabTitle);
tab8.setTabListener(this);
actionBar.addTab(tab8);
tabNumber++;
break;
case 9:
ActionBar.Tab tab9=actionBar.newTab();
tab9.setText(addTabTitle);
tab9.setTabListener(this);
actionBar.addTab(tab9);
tabNumber++;
break;
case 10:
ActionBar.Tab tab10=actionBar.newTab();
tab10.setText(addTabTitle);
tab10.setTabListener(this);
actionBar.addTab(tab10);
tabNumber++;
break;
}
}
}
}
private void GetTopics() {
// TODO Auto-generated method stub
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "topic" + addTabPosition + ".json");
Log.e("",file.toString());
if (!file.exists()) { // Checks if file exists
addTabMessage = "FileNotFound";
Log.e("NOT FOUND ",file.toString());
}else{
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
addTabTitle = br.readLine();
fileNames[addTabPosition] = file;
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The problem was that the string addTabMessage was not being reset each time. So once it was set it remained with that value.