0
I have this code, which takes Strings and puts them in a Database, but the problem is that it is creating Database but I don’t know why , it is not inserting the values in it. Below I leave the xml and java files of Activity that I am trying to remove the values, and also leave the java document of Sqliteopenhelper . I appreciate your help.
Tab1testes.java :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.tab1tests, container, false);
add_test = (Button) rootView.findViewById(R.id.add_test);
date_button = (Button) rootView.findViewById(R.id.date_button);
hora_text = (TextView) rootView.findViewById(R.id.hora_text);
hora_button = (Button) rootView.findViewById(R.id.hora_button);
date_text = (TextView) rootView.findViewById(R.id.date_text);
sala_text = (EditText) rootView.findViewById(R.id.sala_text);
myDB = new DataBaseHelper(getContext());
date_button.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog date_dialog = new DatePickerDialog(
getActivity(),
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
dateSetListener,
year, month, day);
date_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
date_dialog.show();
}
;
}
);
dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
date_button.setVisibility(View.GONE);
date_text = (TextView) rootView.findViewById(R.id.date_text);
date_text.setVisibility(View.VISIBLE);
dt = day + "-" + month + "-" + year;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
try {
sdf.parse(dt);
date_text.setText(dt);
} catch (ParseException e) {
e.printStackTrace();
}
}
};
hora_button = (Button) rootView.findViewById(R.id.hora_button);
hora_button.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int hora = cal.get(Calendar.HOUR);
int minutos = cal.get(Calendar.MINUTE);
TimePickerDialog mTimePicker = new TimePickerDialog(getActivity(), TimePickerDialog.THEME_HOLO_LIGHT, hourSetListener, hora, minutos, true);
mTimePicker.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mTimePicker.show();
}
}
);
hourSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hora, int minutos) {
hora_button.setVisibility(View.INVISIBLE);
hora_text.setVisibility(View.VISIBLE);
hora2 = (hora + ":" + minutos);
hora_text.setText(hora2);
}
};
hora_text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int hora = cal.get(Calendar.HOUR);
int minutos = cal.get(Calendar.MINUTE);
TimePickerDialog mTimePicker = new TimePickerDialog(getActivity(),TimePickerDialog.THEME_HOLO_LIGHT,hourSetListener,hora,minutos,true);
mTimePicker.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mTimePicker.show();
}
});
date_text.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog date_dialog = new DatePickerDialog(
getActivity(),
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
dateSetListener,
year, month, day);
date_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
date_dialog.show();
}
});
addData();
return rootView;
}
public void addData(){
add_test.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
boolean isInserted = myDB.insertData("Portugues", sala_text.getText().toString(), dt, hora2);
if (isInserted==true){
Toast.makeText(getContext(),"Teste inserido ao calendario",Toast.LENGTH_LONG);
date_button.setVisibility(View.VISIBLE);
hora_button.setVisibility(View.VISIBLE);
date_text.setVisibility(View.INVISIBLE);
sala_text.setText("");
}
else{
Toast.makeText(getContext(),"Preencha todos os espaços",Toast.LENGTH_LONG);
}
}
});
}
}
Tab1testes.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:focusable="true"
android:focusableInTouchMode="true">
<Button
android:id="@+id/add_test"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginLeft="67dp"
android:text="Adicionar o teste"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"
android:layout_below="@+id/Hora"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp" />
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/disciplina"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/section_label"
android:layout_margin="20dp"
android:layout_toEndOf="@+id/section_label"
android:text="Disciplina:"
android:textSize="35sp" />
<TextView
android:id="@+id/sala"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/disciplina"
android:layout_margin="20dp"
android:layout_toEndOf="@+id/section_label"
android:text="Sala:"
android:textSize="35sp"/>
<TextView
android:id="@+id/Dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/sala"
android:layout_margin="20dp"
android:layout_toEndOf="@+id/section_label"
android:text="Dia:"
android:textSize="35sp" />
<TextView
android:id="@+id/Hora"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Dia"
android:layout_margin="20dp"
android:layout_toEndOf="@+id/section_label"
android:text="Hora:"
android:textSize="35sp" />
<TextView
android:id="@+id/date_text"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="ESCOLHER DATA"
android:textSize="25sp"
android:textStyle="bold"
android:visibility="gone"
android:layout_alignBaseline="@+id/Dia"
android:layout_alignBottom="@+id/Dia"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/Dia" />
<Button
android:id="@+id/date_button"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/hora_button"
android:layout_alignTop="@+id/Dia"
android:text="ESCOLHER DATA"
android:textSize="25sp"
android:textStyle="bold"
android:onClick="setDate"/>
<TextView
android:id="@+id/hora_text"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="ESCOLHER HORA"
android:textStyle="bold"
android:textSize="25sp"
android:layout_alignBottom="@+id/Hora"
android:layout_toEndOf="@+id/Hora"
android:visibility="gone"/>
<Button
android:id="@+id/hora_button"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="ESCOLHER HORA"
android:textStyle="bold"
android:textSize="25sp"
android:layout_alignBaseline="@+id/Hora"
android:layout_toEndOf="@+id/sala" />
<EditText
android:id="@+id/sala_text"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/sala"
android:layout_toEndOf="@+id/Hora"
android:ems="10"
android:hint="Nª/Nome da sala"
android:textSize="25sp"
android:inputType="textPersonName" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/disciplina"
android:layout_toEndOf="@+id/disciplina"
android:scrollbarSize="25sp"
android:layout_alignTop="@+id/disciplina" />
Databasehelper:
public class DataBaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Exames.db";
public static final String TABLE_NAME = "Exames_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "DISCIPLINA";
public static final String COL_3 = "SALA";
public static final String COL_4 = "DIA";
public static final String COL_5 = "HORA";
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(" create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT , DISCIPLINA TEXT , SALA TEXT,DIA TEXT)" );
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(sqLiteDatabase);
}
public boolean insertData(String disciplina,String sala,String dia,String hora){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_2,disciplina);
values.put(COL_3,sala);
values.put(COL_4,dia);
values.put(COL_5,hora);
long result = sqLiteDatabase.insert(TABLE_NAME,null,values);
if(result==-1){
return false;
}else {
return true;
}
}
}