2016-04-20 39 views
0

我已經爲學生考勤應用程序註冊活動,其中教師只有在「教師複選框」被選中時才註冊。教師信息應添加到教師表中。如果複選框沒有被選中,那麼默認情況下,信息將被輸入到學生表。所以1個數據庫和2個表。如何將數據添加到另一個表,只有當選中複選框使用Sqlite數據庫? - android studio

This is the register activity:

這是數據庫輔助代碼,我想將數據添加到教師表只有當教師複選框在insertData方法檢查..但我不知道如何做到這一點。我是android studio的新手。

import android.content.ContentValues; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 


public class DatabaseHelper extends SQLiteOpenHelper { 

public static final String DATABASE_NAME ="Register.db"; 
public static final String TABLE_STUDENT ="Register_student"; 
public static final String TABLE_FACULTY ="Register_faculty"; 
public static final String COL_1 ="Name"; 
public static final String COL_2 ="Username"; 
public static final String COL_3 ="Password"; 
private static final String CREATE_TABLE_STUDENT = "CREATE TABLE " 
     + TABLE_STUDENT + "(" + COL_1 
     + " TEXT," + COL_2 + "TEXT," + COL_3 
     + " TEXT" + ")"; 

private static final String CREATE_TABLE_FACULTY = "CREATE TABLE " 
     + TABLE_FACULTY + "(" + COL_1 
     + " TEXT," + COL_2 + "TEXT," + COL_3 
     + " TEXT" + ")"; 

public DatabaseHelper(Context context) { 
    super(context,DATABASE_NAME, null, 1); 

} 

@Override 
public void onCreate(SQLiteDatabase db) { 

    db.execSQL(CREATE_TABLE_STUDENT); 
    db.execSQL(CREATE_TABLE_FACULTY); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENT); 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_FACULTY); 

    onCreate(db); 
} 

public void insertData(String name, String username, String password) 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COL_1,name); 
    contentValues.put(COL_2,username); 
    contentValues.put(COL_3, password); 
    db.insert(TABLE_STUDENT,null,contentValues); 
    } 
} 

我register.java頁

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.Button; 
import android.view.View; 
import android.content.Intent; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.Toast; 

public class register extends AppCompatActivity { 
Button register; 
CheckBox faculty; 
EditText editName, editUName, editPass; 
DatabaseHelper myDB; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_register); 
    myDB = new DatabaseHelper(this); 
    editName = (EditText) findViewById(R.id.editText3); 
    editUName = (EditText) findViewById(R.id.editText7); 
    editPass = (EditText) findViewById(R.id.editText5); 
    faculty = (CheckBox) findViewById(R.id.checkBox38); 
    register = (Button) findViewById(R.id.button2); 
    register(); 
} 
public void register() { 
    register.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      boolean isInserted= 
myDB.insertData(editName.getText().toString(), 
        editUName.getText().toString(), 
    editPass.getText().toString(), faculty.isChecked()); 
    if(isInserted==true) 
{ 
Toast.makeText(getApplicationContext(), "Data added",  
Toast.LENGTH_SHORT).show(); 
} 
      else 
    { 
Toast.makeText(getApplicationContext(), "Data not added",  
Toast.LENGTH_SHORT).show(); 
    } 
      Intent i = new Intent(register.this, login.class); 
      startActivity(i); 

      Toast.makeText(getApplicationContext(), "You have been  
registered", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    } 
} 

回答

1

設置一個整數標誌0如果未選中複選框,1如果複選框被選中。 發送此標誌insertData()功能,如下圖所示:

public void insertData(String name, String username, String password, int flag) 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COL_1,name); 
    contentValues.put(COL_2,username); 
    contentValues.put(COL_3, password); 

    if(flag==0) 
    { 
     db.insert(TABLE_STUDENT,null,contentValues); 
    } 
    else if(flag==1) 
    { 
     db.insert(TABLE_FACULTY,null,contentValues); 
    } 
} 
1

以布爾變量,如果複選框被選中設置爲true否則爲false選中。像這樣將變量發送到insertData()函數。

public void insertData(String name, String username, String password, boolean checked) { 
SQLiteDatabase db = this.getWritableDatabase(); 
ContentValues contentValues = new ContentValues(); 
contentValues.put(COL_1,name); 
contentValues.put(COL_2,username); 
contentValues.put(COL_3, password); 

if(!checked) 
    db.insert(TABLE_FACULTY,null,contentValues); 
else 
    db.insert(TABLE_STUDENT,null,contentValues); 

} // 註冊頁面

buttonRegister.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      if (checkbox.isChecked()) { 
       //checkbox checked 
      } else { 
       //checkbox unchecked 
      } 
     } 
    }); 
+0

它的工作!感謝名單。但我唯一的問題是,當我在register.java頁面的insertData方法中傳遞布爾值時,只有在複選框被選中的情況下才會添加數據。數據僅添加給教師而不是學生。我怎樣才能修復它,使數據被添加到學生的表中,當checkBox未選中時? – beginner

+0

看到我編輯的答案 –

相關問題