數組適配器需要的對象的列表作爲第三個參數作爲公共構造文檔建議(來源Developer Android):
Constructor
ArrayAdapter(Context context, int textViewResourceId, T[] objects)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
Constructor
ArrayAdapter(Context context, int textViewResourceId, List<T> objects)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
所以你只需要封裝你String將數組(即使你只有一個字符串可以顯示)。
ArrayList<String> arr = new ArrayList<String>();
arr.add(s);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, arr));
編輯 確定。你爲什麼不嘗試一個非常基本的if-else語句?喜歡:
ArrayList<String> arrButton1 = new ArrayList<String>();
ArrayList<String> arrButton2 = new ArrayList<String>();
ArrayList<String> arrToChoose;
if(s.equals("button1")) {
arrToChoose = arrButton1;
} else if(s.equals("button2")) {
arrToChoose = arrButton2;
}
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, arrayToChoose));
我試過了,但後來它只顯示我的變量。如果s =「button1」,如果我將它封裝在數組中,唯一出現在listView中的是button1。我試圖在這個活動中加載一個名爲button1的字符串數組的內容。 – Roger 2011-02-23 22:24:03
好的。我希望我明白你想要解決什麼問題。基本上你想顯示一個或另一個數組,取決於用戶是通過點擊一個按鈕還是另一個來進行該活動?看到我的編輯... – 2011-02-23 22:35:19
這工作,謝謝。 – Roger 2011-02-23 22:42:25