我正在尝试创建一个Spinner
,其中填充了我在Java中创建的Array
数据,该数据从文本文件中读取。
(它被称为pokemon.txt,所以这是720个条目..单独输入这些是愚蠢的,所以我觉得使用文本文件并从中读取将是最佳的。)
Spinner spinner;
public static void main(String[] args) throws IOException {
List<String> lines = new ArrayList<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("pokemon.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} finally {
reader.close();
}
String[] array = (String[]) lines.toArray();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
Typeface Dotum = Typeface.createFromAsset(getAssets(),"fonts/gulim.ttc");
TextView myTextView = (TextView)findViewById(R.id.jingenMenu1);
myTextView.setTypeface(Dotum);
spinner=(Spinner) findViewById(R.id.pkmnSel1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, , android.R.layout.simple_spinner_item); //I know it's missing the source but, that's where I'm
spinner.setAdapter(adapter); //lost because I can't figure out how to populate it with my array.
}
我知道我的代码可能非常混乱和/或不正确但是,这有点过头了,所以我真的想尽可能多地了解这个。
如果有一种更简单的方法,甚至可能是将文件直接读入XML数组的方法,请随时告诉我。一切都有帮助。
感谢阅读,并且提前非常感谢。
答案 0 :(得分:0)
Android中没有main()
功能,如上面提到的@codeMagic。 onCreate()
是在创建应用程序时运行的函数。您可以创建一个新函数,为Spinner返回Array并在onCreate()
中调用它,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
Typeface Dotum = Typeface.createFromAsset(getAssets(),"fonts/gulim.ttc");
TextView myTextView = (TextView)findViewById(R.id.jingenMenu1);
myTextView.setTypeface(Dotum);
String[] array = yourFunctionThatReturnsArray();
spinner=(Spinner) findViewById(R.id.pkmnSel1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,array , android.R.layout.simple_spinner_item); //I know it's missing the source but, that's where I'm
spinner.setAdapter(adapter); //lost because I can't figure out how to populate it with my array.
}
public String[] yourFunctionThatReturnsArray(){
String[] array = new String[SIZE];
// put anything in array
return array;
}