How to store dropdown item of the spinner to the sqlite database of android
I want to store the dropdown value of the spinner to the database. I am able to get dopdown as per the tutorial in the android developer site but i am not able to store that dropdown value to the database 开发者_如何学JAVAwhen user click on save button.I don't know it is possible or not if yes please tell me how to do that with some example. Thanks in advance
This is my code
public class Akshay extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Spinner For Selecting Room
Spinner spinner_room = (Spinner) findViewById(R.id.spinner_for_Room_type_screen_2);
ArrayAdapter adapter_room = ArrayAdapter.createFromResource(this,
R.array.room_array, android.R.layout.simple_spinner_item);
adapter_room.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_room.setAdapter(adapter_room);
spinner_room.setOnItemSelectedListener(new MyOnItemSelectedListener_room());
}
}
// Listener Implementation of Spinner For Selecting Room
public class MyOnItemSelectedListener_room implements OnItemSelectedListener
{
public void onItemSelected(AdapterView parent, View view, int pos, long id)
{
}
public void onNothingSelected(AdapterView parent)
{ // Do nothing.}
};
}
public class Akshay extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.configuration);
// Spinner For Selecting Room
Spinner spinner_room = (Spinner) findViewById(R.id.spinner_for_Room_type_screen_2);
ArrayAdapter<CharSequence> adapter_room = ArrayAdapter.createFromResource(this,
R.array.room_array,android.R.layout.simple_spinner_item);
adapter_room.setDropDownViewResourc(android.R.layout.simple_spinner_dropdown_item);
spinner_room.setAdapter(adapter_room);
spinner_room.setOnItemSelectedListener(new Listener_Of_Selecting_Room_Spinner());
}
// Listener Implementation of Spinner For Selecting Room
public static class Listener_Of_Selecting_Room_Spinner implements OnItemSelectedListener
{
static String RoomType;
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id)
{
// By using this you can get the position of item which you
// have selected from the dropdown
RoomType = (parent.getItemAtPosition(pos)).toString();
}
public void onNothingSelected(AdapterView<?> parent)
{
// Do nothing.
}
};
// Listener Implementation For Saving Number Of Board
private OnClickListener btnListener_Btn_Save_Room_Board = new OnClickListener()
{
public void onClick(View view)
{
DBAdapter dbAdapter1 = new DBAdapter(view.getContext());
String room;
try {
dbAdapter1.createDataBase();
dbAdapter1.openDataBase();
// Here i am using the object RoomType which i have got from
// the Listener of spinner
room = Listener_Of_Selecting_Room_Spinner.RoomType;
ContentValues initialValues1 = new ContentValues();
initialValues1.put("RoomType", room);
//Here i am storing it(RoomType) to the database
dbAdapter1.InsertNumberOfBoardInDB("Configuration", null,initialValues1);
}
catch (Exception e) {
}
finally {
dbAdapter1.close();
}
}
};
}
If you're looking for information on how to use SQLite databases in an Android app, I highly recommend going through the Notepad tutorial.
Or are you stuck at handling the button click?
This doesn't directly answer the question, but design-wise and for simplicity's sake it may make some sense to store UI values as preference.
For example:
public static final String PREFS_NAME = "MyPrefsFile";
public static final String SPINNER_VALUE = "SPINNER VALUE";
SharedPreferences settings = getSharedPreferences(PREFS_NAME , 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(SPINNER_VALUE, <the spinner value>);
editor.commit();
Of course this depends on your requirement, ymmv.
精彩评论