Android: Spinner autorefreshing itself - How to?
I have made the following Android widget:
A spinner "Location" populated from the sqlite table "Location", which pops an EditText dialog on a long touch, and which saves the EditText' valie to the "Location" table, on dismissing the popup dialog.
Now I would like to have the spinner autorefresh itself with the new information I have just entered, and have no idea how to proceed - here is my current code
chooseLocation = (Spinner) this.findViewById(R.id.choose_location);
//set long clicks to add an item
chooseLocation.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
//just an EditText dialog
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
final EditText input = new EditText(MyActivity.this);
builder.setView(input);
builder.setMessage(R.string.add_location)
.setCancelable(false)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (input.getText().toString().equals("") == false) {
//save EditText to database
db.open();
db.insertLocation(input.getText().toString());
db.close();
}
dialog.cancel();
}})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
开发者_运维技巧}})
.show();
return true;
}});
//Binding the database data and the spinner
Cursor locations = db.getLocations();
startManagingCursor(locations);
SimpleCursorAdapter adapter1 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, locations,
new String[] { "_name"}, new int[] { android.R.id.text1});
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
chooseLocation.setAdapter(adapter1);
mb.close();
i'm not so good in SQLLite, but i think , when you insert the new value into the table Location, you can send another request to select from location , and display it in your Spinner , or something like that
Perhaps you need to tell the Adapter to refresh itself after you set it on the Spinner with a call to
adapter1.notifyDataSetChanged();
精彩评论