2015-09-28 47 views
0

我試圖創建一個Spinner,該數據填充了我從一個文本文件讀取的Java中創建的Array的數據。如何用我在Java中創建的數組填充微調框?

(這就是所謂的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數組的方式,那麼請隨時告訴我。任何幫助。

感謝您的閱讀,並且非常感謝您的提前。

+1

Android與Java不同。沒有'main()'入口點。你需要在'onCreate()'的其他地方調用這個代碼,然後你可以在數組填充後加載你的微調器。 – codeMagic

+0

Oooooooh!我懂了!這意味着很多。我非常感謝你的幫助。 :d – searequiem

回答

0

Android中沒有main()函數,就像上面提到的@codeMagic一樣。 onCreate()是創建應用程序時運行的函數。你可以創建一個新的函數返回Array for Spinner,並在你的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; 
}