到目前为止,我的项目看起来像这样。
MainActivity.java
package com.example.soultaker.myovertimesapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.view.View;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.btn_add);
Button button1 = (Button) findViewById(R.id.btn_logs);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AddActivity.class);
startActivity(intent);
}
});
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, LogsActivity.class);
startActivity(intent);
}
});
}
}
activity_main.xml
<TextView
android:id="@+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:layout_width="347dp"
android:layout_height="96dp"
android:id="@+id/dobPicker"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
android:calendarViewShown="false"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintHorizontal_bias="0.61" />
<EditText
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/addHoursText"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
app:layout_constraintVertical_bias="0.39"
android:hint="Εισάγετε Ώρες"
android:textAlignment="center"
style="@style/Widget.AppCompat.EditText"
android:layout_width="145dp"
android:maxLength="2"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:spinnerMode="dialog"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
android:entries="@array/spinnerItems"
android:id="@+id/spinner"
android:textAlignment="center"
android:clipChildren="false"
android:saveEnabled="true"
android:layout_height="40dp"
android:background="@color/colorPrimary"
android:layout_width="100dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:foregroundGravity="center_horizontal"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.6" />
<Button
android:text="Πίσω"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_back"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
app:layout_constraintHorizontal_bias="0.02"
app:layout_constraintVertical_bias="0.96"
android:fontFamily="sans-serif-smallcaps"
android:textAllCaps="false"
android:textSize="18sp" />
<Button
android:text="Εισαγωγή"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_insert"
android:fontFamily="sans-serif-smallcaps"
android:textSize="18sp"
android:textAllCaps="false"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
app:layout_constraintVertical_bias="0.96" />
<Button
android:text="Έξοδος"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintVertical_bias="0.96"
android:fontFamily="sans-serif-smallcaps"
android:textSize="18sp"
android:textAllCaps="false" />
<TextView
android:text="Επιλέξτε Βάρδια:"
android:layout_width="143dp"
android:layout_height="28dp"
android:id="@+id/textView"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
android:fontFamily="sans-serif-smallcaps"
android:textSize="18sp"
android:textAllCaps="false"
android:textAlignment="center" />
<Button
android:text="Αλλαγή Ημερομηνίας"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnChangeDate"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
app:layout_constraintVertical_bias="0.25"
android:fontFamily="sans-serif-smallcaps"
android:textSize="18sp"
android:textAllCaps="false" />
</android.support.constraint.ConstraintLayout>
DatabaseHelper.java
package com.example.soultaker.myovertimesapplication;
/**
* Created by Soultaker on 17/2/2017.
*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHandler {
public static final String KEY_ROWID = "_id";
public static final String KEY_DATE = "date";
public static final String KEY_SHIFT = "shift";
public static final String KEY_HOURS = "hours";
private static final String TAG = "DatabaseHandler";
private static final String DATABASE_NAME = "MyDB";
private static final String DATABASE_TABLE = "Overtimes";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table Overtimes (_id integer primary key autoincrement, "
+ "date text not null, shift text not null, hours text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DatabaseHandler(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS Overtimes");
onCreate(db);
}
}
//---opens the database---
public DatabaseHandler open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a Overtime into the database---
public long insertOvertime(String date, String shift, Integer hours)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DATE, date);
initialValues.put(KEY_SHIFT, shift);
initialValues.put(KEY_HOURS, hours);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular Overtime---
public boolean deleteOvertimes(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
//---retrieves all the Overtimes---
public Cursor getAllOvertimes()
{
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_DATE, KEY_SHIFT, KEY_HOURS},
null, null, null, null, null);
}
//---retrieves a particular Overtimes---
public Cursor getOvertime(long rowId) throws SQLException
{
Cursor mCursor =db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_DATE, KEY_SHIFT, KEY_HOURS}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a Overtimes---
public boolean updateOvertime(long rowId, String date, String shift, Integer hours)
{
ContentValues args = new ContentValues();
args.put(KEY_DATE, date);
args.put(KEY_SHIFT, shift);
args.put(KEY_HOURS, hours);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
AddActivity.java
package com.example.soultaker.myovertimesapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.Dialog;
import static com.example.soultaker.myovertimesapplication.R.array.spinnerItems;
public class AddActivity extends AppCompatActivity {
private TextView tvDisplayDate;
private DatePicker dpResult;
private Button btnChangeDate,btnback,btninset;
private int year;
private int month;
private int day;
static final int DATE_DIALOG_ID = 999;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
setCurrentDateOnView();
addListenerOnButton();
final Button btnback = (Button) findViewById(R.id.btn_back);
final Button btninsert = (Button) findViewById(R.id.btn_insert);
final Spinner shift = (Spinner) findViewById(R.id.spinner);
final EditText hours = (EditText) findViewById(R.id.addHoursText);
final DatabaseHandler db = new DatabaseHandler(AddActivity.this);
btninsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
db.open();
db.insertOvertime(date.getText().toString(),//I CANT FIND A WAY TO GET DATE AS STRING AND PUT IT AS TEXT?//
shift.getItemAtPosition(spinnerItems).toString(),
hours.getText().toString());
db.close();
Toast.makeText(getBaseContext(), "Inserted",
Toast.LENGTH_SHORT).show();
}
});
//---Select All Overtimes---
btnback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(AddActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
dpResult = (DatePicker) findViewById(R.id.dpResult);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
// set current date into datepicker
dpResult.init(year, month, day, null);
}
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener,
year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener
= new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into textview
tvDisplayDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
// set selected date into datepicker also
dpResult.init(year, month, day, null);
}
};
}
非常感谢你的建议,我能够走得很远。只有一个问题仍然存在,我需要你的帮助。
答案 0 :(得分:0)
首先,您在此处遇到问题:您正在创建表设计中不存在的未使用的列!
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
你需要修改它来创建一个包含从COL_0到COL_5的所有列的表,如下所示:
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME
+ "("
+ COL_0 +" INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_1 + " TEXT, "
+ COL_2 + " TEXT, "
+ COL_3 + " TEXT, "
+ COL_4 + " TEXT, "
+ COL_5 + " TEXT "
+ ")"
);
我想知道如何制作该按钮
只需向您添加一个Button xml,然后按如下方式绑定:onCreate
Button button = (Button) findValueById(R.id.YOUR_BUTTON_ID_IN_XML);
然后在此按钮上设置onClick
侦听器,如下所示:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//call insert here to insert your data in database
}
});
我是否需要为日,月,年制作单独的列,否则我可以存储 他们是一个人吗?
我会在DB中的一列中保存日期甚至日期和时间
这是为了创建一个名为date
的列TEXT
而不是传递String day,String month,String year
到insertData方法,我将只传递一个创建如下的字符串日期:
String date = day + "/" + month + "/" + year;
并将其保存在列日期!
我也可以在没有setdate按钮的情况下使用datepicker 我不推荐这个,因为它可能对用户来说是一个糟糕的用户体验!
What am I missing?
我发现您的insertData
应该可以正常工作,如果有的话,您可以随时发布新问题!