dimanche 22 février 2015

How to use spinner list in database

Now, i create a spinner list which show in programme and there are several choices. If i want to use an spinner list to select an item then the selected item will add into database by using SQLite code. Also, how to show the selected item in next line after "Amount"?



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

dbhelper = new DBhelper(this);
db = dbhelper.getWritableDatabase();
tEvent = (EditText) findViewById(R.id.event);
tLocation = (EditText) findViewById(R.id.venue);
tAmount = (EditText) findViewById(R.id.amount);

spin = (Spinner) findViewById(R.id.spinner);
list.add("---CATEGORIES---");
list.add("Eating");
list.add("Entertainment");
list.add("Shopping");
list.add("Transportation");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
}

protected void onStop() {
super.onStop();
db.close();
}

public void Badd_Click(View view){
db.execSQL("INSERT INTO Demo (event, venue, amount, textView) VALUES ('" + tEvent.getText().toString() + "'," + "'" + tLocation.getText().toString() + "'," + "'" + tAmount.getText().toString() +"' )");
Toast.makeText(this, "Create record successfully...", Toast.LENGTH_SHORT).show();
}

public void Btnshow_Click(View view){
String str = "";
Cursor c = db.rawQuery("SELECT * FROM "+DATABASE_TABLE,null);
c.moveToFirst();
for(int i = 0; i<c.getCount();i++){
str+="ID: "+c.getString(0)+"\n";
str+="Name : "+c.getString(1)+"\n";
str+="Location : "+c.getString(2)+"\n";
str+="Amount : "+c.getString(3)+"\n\n";
c.moveToNext();
}

Builder MyAlertDialog = new AlertDialog.Builder(this);
MyAlertDialog.setTitle("Records");
MyAlertDialog.setMessage(str);
DialogInterface.OnClickListener OkClick = new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
}
};
MyAlertDialog.setNeutralButton("OK",OkClick );
MyAlertDialog.show();
}


}


Aucun commentaire:

Enregistrer un commentaire