I want to show an item from editext to spinner and save to db... how to store item in the db...
my code :
spinner populating
final DBAdapter db = new DBAdapter(this);
db.open();
Spinner spin = (Spinner) findViewById(R.id.spinner1);
AdapterCountries = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item);
AdapterCountries.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(AdapterCountries);
Cursor cursor = db.getAllTitles1();
while (cursor.moveToNext()){
results=cursor.getString(2);
AdapterCountries.add(results);
}
db.close();
and
Button d_ok=(Button)dialog.findViewById(R.id.d_ok);
final EditText filename=(EditText)dialog.findViewById(R.id.filename);
d_ok.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
//
}});
any one can h开发者_StackOverflowelp me with example
Thank you...
If you don't have one already, then I really think you should have a SQL helper class extending the given SQLiteOpenHelper Android class. It really simplifies DB operations. See: http://developer.android.com/guide/topics/data/data-storage.html#db
It's heavily recommended.
If you set up the helper class and the instance of that class is set up like SQLHelper sql = new SQLHelper(this);
then modifying the database is fairly simple. You should set up a method that you call from your buttons onClickListener (and possibly run it in an AsyncTask or a background thread):
private void addFileName(final String filename) {
SQLiteDatabase db = sql.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(yourKeyHere, filename);
db.insert(yourDBNameHere, null, values);
}
And then call the method and add it to your adapter from the listener:
d_ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addFileName(filename.getText().toString();
AdapterCountries.add(filename);
}
});
精彩评论