【问题标题】:How to use spinner list in database如何在数据库中使用微调器列表
【发布时间】:2015-02-22 13:54:31
【问题描述】:

现在,我创建了一个在程序中显示的微调器列表,并且有多种选择。如果我想使用微调器列表来选择一个项目,那么所选项目将使用 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();
}

}

【问题讨论】:

    标签: android database list sqlite spinner


    【解决方案1】:

    使用 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();
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多