0
所以即時通訊的初學者在Android/Java編程和即時通訊嘗試執行以下操作:從我的微調選擇的項目應該在列表視圖中,當我點擊一個按鈕。到目前爲止我這樣做,日食犯規顯示任何錯誤或警告,但我的應用程序崩潰,當我試圖啓動它:微調元素listview上點擊應用程序崩潰(android)
public class MainActivity extends ActionBarActivity {
Button button3;
Button button2;
Button button4;
ListView listView1;
Spinner s1;
String text;
ArrayAdapter<String> adapter;
ArrayList<String> list;
int itemPos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button2 = (Button) findViewById(R.id.button2);
s1 = (Spinner) findViewById(R.id.spinner1);
listView1 = (ListView) findViewById(R.id.listView1);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, list); //this part here might not be correct
listView1.setAdapter(adapter);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String text = s1.getSelectedItem().toString();
adapter.add(text);
}});
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
itemPos = position;
}
});}}
我不知道爲什麼會崩潰,如何解決?
謝謝!
謝謝!它解決了我的問題!基本上,當我點擊一個按鈕時,如何從列表視圖中刪除選定的項目?謝謝 – 2014-11-02 19:50:56
list.remove(text);應該管用。但不要忘記調用adapter.notifyDataSetChanged();. – Chitrang 2014-11-02 19:55:00