现在,我创建一个在程序中显示的微调器列表,有几个选项。如果我想使用微调器列表来选择项目,那么所选项目将使用SQLite代码添加到数据库中。另外,如何在“金额”之后的下一行显示所选项目?
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();
}
}
答案 0 :(得分:0)
使用OnItemSelectedListener
private String category;
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);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View view, int pos, long id) {
category = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView parent) {
category = null;
}
});
}
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() + "' )");
db.execSQL("INSERT INTO Demo (category) VALUES ('" + category + "');"); // ONLY EXAMPLE
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();
}