使用SQLite数据库更新Spinner

时间:2016-11-13 13:40:48

标签: android

我需要在添加显示的数据后不断更新我的Spinner 我的部分代码:

public class MainActivity extends AppCompatActivity {  
ArrayAdapter<String> adapter;  
Spinner spinner;  

protected void onCreate(Bundle savedInstanceState) {
ArrayList<String> categories = databaseHelper.getAllCategories();
spinner = (Spinner) findViewById(R.id.spinner);
adapter =  new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, categories);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}

在DatabaseHelper类中,我进行简单查询,选择一列。

1 个答案:

答案 0 :(得分:2)

public class MainActivity extends AppCompatActivity {  
ArrayAdapter<String> adapter;  
Spinner spinner;
ArrayList<String> categories;  

protected void onCreate(Bundle savedInstanceState) {
categories = databaseHelper.getAllCategories();
spinner = (Spinner) findViewById(R.id.spinner);
adapter =  new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, categories);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}

private function updateSpinner(){
    categories.clear();
    categories.addAll(databaseHelper.getAllCategories());
    adapter.notifyDataSetChanged();
}