0
我創建了這個簡單的android活動來演示我的問題。在屏幕上添加ListView並用按鈕單擊添加元素
我只想讓屏幕有一個textInput和一個按鈕。下面這兩個,我想創建一個ListView,如果按鈕被按下(該按鈕基本上調用一些方法並獲得一個String數組,我想讓ListView顯示該數組。 ,文本輸入時,按下按鈕它調用的方法和接收字符串數組,並希望打印列表它們下面。
public class TagListViewer extends ListActivity {
private Button clickBtn;
EditText textInput;
String[] resultStr = {"a", "b", "c"}; //Ideally would want this inside the button OnClickListener... but couldn't bcz I needed one for the Array adapter.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tagselection);
clickBtn = (Button) findViewById(R.id.CreatePL);
clickBtn.setText("Search");
textInput = (EditText) findViewById(R.id.textInput);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, resultStr);
setListAdapter(adapter);
clickBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ArrayAdapter<String> adapter = (ArrayAdapter<String>) getListAdapter();
adapter.add("ABC"); //This I could use the array I get to add its elements
adapter.notifyDataSetChanged();
}
});
}
}
是的,這基本上我想,只是有什麼需要的resultStr這不是問題。但是當我運行它時,它給了我RunTimeException:「你的內容必須有一個ListView,其id屬性是'android.R.id.list」btw我凸輪從另一個屏幕調用整個屏幕,這可能是原因嗎? – Achilles 2012-08-13 21:24:21
這個錯誤僅僅意味着你的佈局tagselection.xml需要一個帶有屬性「android:id =」@ android.R.id.list「'的ListView。 – Sam 2012-08-13 21:38:32
讓我試試這個,看看結果,對不起,我把它添加到你的代碼中,這樣你就可以看到,但我們可以修復,只要我找到最終的解決方案 – Achilles 2012-08-13 22:06:16