【问题标题】:Update Spinner items to the database.?将 Spinner 项目更新到数据库。?
【发布时间】:2012-12-12 23:22:47
【问题描述】:

我有一个从 SQLite 数据库加载项目的微调器。它工作正常。 然后我有一个edittext 来将选定的微调器项目添加到该edittext。它也可以。 然后我想更改编辑文本值并将该微调器项目更新到数据库。 那么如何做到这一点。请任何帮助。谢谢

【问题讨论】:

    标签: android database sqlite spinner


    【解决方案1】:

    您应该进行 sql 更新查询。由于您已经从 DB 中获得了微调项,因此您应该知道如何进行查询。更新查询需要调用db.update()方法。

    【讨论】:

      【解决方案2】:

      比方说,您已经使用数据库中的数据填充了 Spinner spinner。并且您有 EditText editTextButton button 来更新微调器值。然后您可以实现如下所示:

      ...
      spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
      
              @Override
              public void onItemSelected(AdapterView<?> arg0, View arg1,
                      int position, long arg3) {
      
                      //code to display the selected item in EditText
              }
      
              @Override
              public void onNothingSelected(AdapterView<?> arg0) {
      
              }
      });
      selectDate.setOnClickListener(new OnClickListener() {
      
              @Override
              public void onClick(View v) {
      
                       DatabaseCreator dbcreator=new DatabaseCreator(context);
                       SQLiteDatabase sqdb=dbcreator.getWritableDatabase();   
      
                       ContentValues values=new ContentValues();
      
                       //Update respective fields in database 
                       values.put("field_1",newValue1);
                       values.put("field_2",newValue2);
      
                       long suceess=sqdb.update( "table_name", values, KEY_ID+"='"+id+"'", null);
                       // you can place any condition in place of KEY_ID+"='"+id+"'" as third argument in above statement
      
                       if(success!=0)
                       {
                            //repopulate spinner here
                       }  
              }
      });
      ...
      

      DatabaseCreator.class:

      public class DatabaseCreator extends  SQLiteOpenHelper{
      
          private static final String DB_NAME="db_name";
          private static final int DB_VER=2;
      
          private static final String TABLE_1="create table table_1(...);"; //complete create query goes here
      
          public DatabaseCreator(Context context) {
              super(context,DB_NAME, null, DB_VER);
          }
      
          @Override
          public void onCreate(SQLiteDatabase database) {
      
              database.execSQL(TABLE_1);      
          }
      
          @Override
          public void onUpgrade(SQLiteDatabase database, int arg1, int arg2) {
      
                  database.execSQL("DROP TABLE IF EXISTS db_name");
              onCreate(database);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-24
        相关资源
        最近更新 更多