To my knowledge, string arrays in Android are not dynamic. At least according to what I've been told on this site. I can't just create a string array and then add as many items to it whenever I feel like it. To get around that, you do a ListArray and then convert it to a String array when you've completed the array. If I was misinformed about this, then you can answer this question by telling me how to create a dynamic String array for example:
String[] menuList = new String[];
menuList.add("One");
menuList.add("A");
String firstItem = menuList[0];
menuList.add("Two");
Notice I can pull from it and then add to it without having to take any additional steps or conversions.
Assuming the above isn't possible, I am trying to set the array based on data in an online text file. So far so good, and each time they access this text file, the app saves the text file locally within the app. Still good. However if there is no internet access, I want the String array to be based on the saved local text file. The problem is that I believe I need to declare the length of the string array as it is being created, and if I create it within the if statement, the rest of the app won't recognize it. Like this:
ListArray<String> menuList = getFile(filename);
//The above line populates the ListArray with the online file, and returns an empty ListArray if there is no internet connection or if there is some problem with getting the file.
//Now I want to populate a String[] with the ListArray if it was successful (and overwrite the local copy), or if it was unsuccessful, I want to get the local copy and populate it with that.
if(menuList.size != 0){
writeToFile("Menu List", menuList);
String[] menuArray = menuList.toArray(new String[menuList.size()]);
} else {
String[] menuArray = readFromFile("Menu List");
}
setUpView(menuArray);
Now obviously this won't work because setUpView can't see that menuArray was created since it is inside an if statement. How can I get use String array's (without simply using ListArray instead) whiles using an if statement to determine its content?
I should mention, since now I've written out the problem, I already see several work arounds. For example, I can simply combine the write and read methods and then I won't need to use the if statement here. Or I could just work with the ListArray until the last minute, or set the String[] to be far longer than I need it to be and stop pulling from it when it gets to an empty slot. That said, using String[] in an if statement has come up before, and I'd like to know how to implement such a thing if it is possible. Thoughts?