I have int values into List<Integer>
I created new random - Random rand = new Random();
And i can get my random value from this
int random_one = givenList.get(rand.nextInt(givenList.size()));
But i need to insert random_one
to the DataBase
As result, i was converted my random_one
to String
String random = Integer.toString(random_one);
But it doesn't work again!
I left my code of my MainActivity Class and my DataBase in this order below
public class MainActivity extends AppCompatActivity {
DataBaseHelper myDb;
EditText name_edittext,age_edittext,weight_edittext,height_edittext;
Spinner gender_spinner,trauma_spinner,illnesses_spinner;
Button save;
String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*DataBase*/
myDb = new DataBaseHelper(this);
/*Spinners*/
gender_spinner = (Spinner) findViewById(R.id.spinner_gender);
trauma_spinner = (Spinner) findViewById(R.id.spinner_trauma);
illnesses_spinner = (Spinner) findViewById(R.id.spinner_illnesses);
String [] gender={"Male","Female"};
ArrayAdapter<String> gender_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,gender);
gender_spinner.setAdapter(gender_adapter);
String [] trauma={"Back","Hand","Leg","None"};
ArrayAdapter<String> trauma_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,trauma);
trauma_spinner.setAdapter(trauma_adapter);
String [] illnesses={"Heart problems","Scoliosis","Others","None"};
ArrayAdapter<String> illnesses_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,illnesses);
illnesses_spinner.setAdapter(illnesses_adapter);
/*EditTexts*/
name_edittext = (EditText) findViewById(R.id.name);
age_edittext = (EditText) findViewById(R.id.age);
weight_edittext = (EditText) findViewById(R.id.weight);
height_edittext = (EditText) findViewById(R.id.height);
/*Button*/
save = (Button) findViewById(R.id.save_button);
/*Save*/
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClickMe();
}
});
}
private void ClickMe(){
String name = name_edittext.getText().toString();
String age = age_edittext.getText().toString();
String weight = weight_edittext.getText().toString();
String height = weight_edittext.getText().toString();
String gender = gender_spinner.getSelectedItem().toString();
String trauma = trauma_spinner.getSelectedItem().toString();
String illnesses = illnesses_spinner.getSelectedItem().toString();
//Random
List<Integer> givenList = Arrays.asList(1, 2, 3);
Random rand = new Random();
int random_one = givenList.get(rand.nextInt(givenList.size()));
String random = Integer.toString(random_one);
Boolean result = myDb.insertData(name,age,weight,height,gender,trauma,illnesses,random);
if (result == true){
Toast.makeText(this,"Data is added successfully",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"Data is not added",Toast.LENGTH_SHORT).show();
}
Intent i = new Intent(this,MenuActivity.class);
startActivity(i);
}
}
&&&
public class DataBaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "Schedule.dp";
public static final String TABLE_NAME = "Schedule_table";
/*Col*/
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "AGE";
public static final String COL_4 = "WEIGHT";
public static final String COL_5 = "HEIGHT";
public static final String COL_6 = "GENDER";
public static final String COL_7 = "TRAUMA";
public static final String COL_8 = "ILLNESSES";
public static final String COL_9 = "RANDOM";
public DataBaseHelper(Context context) {
super(context,DATABASE_NAME,null,1);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE "+TABLE_NAME+" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,AGE INTEGER,WEIGHT INTEGER,HEIGHT INTEGER,GENDER TEXT,TRAUMA TEXT,ILLNESSES TEXT,RANDOM TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
}
public boolean updataData(String id,String name,String age,String weight,String height,String gender,String trauma,String illnesses,String random){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,age);
contentValues.put(COL_4,weight);
contentValues.put(COL_5,height);
contentValues.put(COL_6,gender);
contentValues.put(COL_7,trauma);
contentValues.put(COL_8,illnesses);
contentValues.put(COL_9,random);
int result = db.update(TABLE_NAME,contentValues,"ID =?",new String[]{id});
if (result>0){
return true;
}else{
return false;
}
}
public boolean insertData(String name,String age,String weight,String height,String gender,String trauma,String illnesses,String random){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,age);
contentValues.put(COL_4,weight);
contentValues.put(COL_5,height);
contentValues.put(COL_6,gender);
contentValues.put(COL_7,trauma);
contentValues.put(COL_8,illnesses);
contentValues.put(COL_9,random);
long result = db.insert(TABLE_NAME,null,contentValues);
db.close();
if (result==-1){
return false;
}else{
return true;
}
}
public Cursor getAllData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("Select * from "+ TABLE_NAME,null);
return res;
}
public void deleteAll(){
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("delete from "+ TABLE_NAME);
db.close();
}
}
Aucun commentaire:
Enregistrer un commentaire