我要做的是创建一个包含1000个左右条目的字符串数组。而不是做:
public void DefaultGeneric(View v) throws FileNotFoundException{
Random rand = new Random();
int i = rand.nextInt(4) + 0;
int j = rand.nextInt(4) + 0;
String[] SomeThingA = {"yep","okay","nope"}; //I need this array to be much much larger but want a better way of doing it.
String[] SomeThingB = {"test","test1","test2"}; //I need this array to be much much larger but want a better way of doing it.
Toast.makeText(getBaseContext(), SomeThingA[i]+SomeThingB[j], Toast.LENGTH_LONG ).show();
}
声明我的长数组我一直在研究如何使用所有数组值创建.txt
文件,然后使用arraylist。但每次我尝试失败。另外,一旦我弄清楚如何使用arraylist,我就需要使用:
Toast.makeText(getBaseContext(), variable, Toast.LENGTH_SHORT).show
呼唤我的arraylist的特定行。
任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
我建议你创建一个JSON文件,如下所示
[
"entryOne",
"entryTwo",
"...",
"entryN"
]
现在将此文件存储在资源中,并按以下方式加载(感谢https://stackoverflow.com/a/13814551/7274758)
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("file_name.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
}
catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
然后您可以尝试通过JSONArray
创建此JSON字符串的数组JSONArray arr = new JSONArray(loadJSONFromAsset());
要从行中检索字符串,请致电getString - arr.getString (rowNo)