-
-
Save uta-water/02b913db4db5c20f3bde38e417246a07 to your computer and use it in GitHub Desktop.
roomdb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout 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" | |
tools:context=".MainActivity" | |
android:orientation="vertical"> | |
<Space | |
android:layout_width="match_parent" | |
android:layout_height="20dp" /> | |
<TextView | |
android:id="@+id/roomtext1" | |
android:layout_width="300dp" | |
android:layout_height="50dp" | |
android:layout_gravity="center" | |
android:gravity="center" | |
android:textSize="25dp" | |
android:text="日付" /> | |
<TextView | |
android:id="@+id/roomtext2" | |
android:layout_width="300dp" | |
android:layout_height="50dp" | |
android:layout_gravity="center" | |
android:gravity="center" | |
android:textSize="25dp" | |
android:text="2022:12:19:18:59:39" /> | |
<TextView | |
android:id="@+id/roomtextmax" | |
android:layout_width="300dp" | |
android:layout_height="50dp" | |
android:layout_gravity="center" | |
android:gravity="center" | |
android:textSize="25dp" | |
android:text="データ保存数:0" /> | |
<TextView | |
android:id="@+id/roomtextdebug" | |
android:layout_width="300dp" | |
android:layout_height="50dp" | |
android:layout_gravity="center" | |
android:gravity="center" | |
android:textSize="25dp" | |
android:text="" /> | |
<Space | |
android:layout_width="match_parent" | |
android:layout_height="20dp" /> | |
<LinearLayout | |
android:layout_width="300dp" | |
android:layout_height="50dp" | |
android:layout_gravity="center" | |
android:orientation="horizontal"> | |
<Button | |
android:id="@+id/roombtnleft" | |
android:layout_width="100dp" | |
android:layout_height="50dp" | |
android:text="←" /> | |
<TextView | |
android:id="@+id/roomtext3" | |
android:layout_width="100dp" | |
android:layout_height="50dp" | |
android:gravity="center" | |
android:text="1/1" /> | |
<Button | |
android:id="@+id/roombtnright" | |
android:layout_width="100dp" | |
android:layout_height="50dp" | |
android:text="→" /> | |
</LinearLayout> | |
<Button | |
android:id="@+id/roombtnadd" | |
android:layout_width="300dp" | |
android:layout_height="50dp" | |
android:layout_gravity="center" | |
android:text="登録" /> | |
<Button | |
android:id="@+id/roombtndelete" | |
android:layout_width="300dp" | |
android:layout_height="50dp" | |
android:layout_gravity="center" | |
android:text="削除" /> | |
<Button | |
android:id="@+id/roombtnalldelete" | |
android:layout_width="300dp" | |
android:layout_height="50dp" | |
android:layout_gravity="center" | |
android:text="全削除" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uta.utauta.roomdb; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.room.Room; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import java.util.Calendar; | |
import java.util.List; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
public class MainActivity extends AppCompatActivity implements View.OnClickListener { | |
private TextView roomtext1; | |
private TextView roomtext2; | |
private TextView roomtext3; | |
private TextView roomtextdebug; | |
private TextView roomtextmax; | |
private Button roombtnleft; | |
private Button roombtnright; | |
private Button roombtnadd; | |
private Button roombtndelete; | |
private Button roombtnalldelete; | |
private String monthtext; | |
private String daytext; | |
private String hourtext; | |
private String minutetext; | |
private String secondtext; | |
private int listpage = 1; | |
private int listmax = 0; | |
private int listid = 0; | |
private List<RoomEntity> list; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
roomtext1 = findViewById(R.id.roomtext1); | |
roomtext2 = findViewById(R.id.roomtext2); | |
roomtext3 = findViewById(R.id.roomtext3); | |
roomtextdebug = findViewById(R.id.roomtextdebug); | |
roomtextmax = findViewById(R.id.roomtextmax); | |
roombtnleft = findViewById(R.id.roombtnleft); | |
roombtnleft.setOnClickListener(this); | |
roombtnright = findViewById(R.id.roombtnright); | |
roombtnright.setOnClickListener(this); | |
roombtnadd = findViewById(R.id.roombtnadd); | |
roombtnadd.setOnClickListener(this); | |
roombtndelete = findViewById(R.id.roombtndelete); | |
roombtndelete.setOnClickListener(this); | |
roombtnalldelete = findViewById(R.id.roombtnalldelete); | |
roombtnalldelete.setOnClickListener(this); | |
loaddb(); | |
} | |
private void loaddb() { | |
ExecutorService executor = Executors.newSingleThreadExecutor(); | |
executor.execute(new Runnable() { | |
@Override | |
public void run() { | |
RoomTimeDatabase db = Room.databaseBuilder(getApplicationContext(), | |
RoomTimeDatabase.class, "roomtime").build(); | |
RoomTimeDao dao = db.roomtimedao(); | |
list = dao.getall(); | |
listmax = list.size(); | |
if(list.size() > 0) { | |
RoomEntity entity = list.get(listpage - 1); | |
int year = entity.getyear(); | |
int month = entity.getmonth(); | |
if(month < 10) { | |
monthtext = "0" + month; | |
} else { | |
monthtext = "" + month; | |
} | |
int day = entity.getday(); | |
if(day < 10) { | |
daytext = "0" + day; | |
} else { | |
daytext = "" + day; | |
} | |
int hour = entity.gethour(); | |
if(hour < 10) { | |
hourtext = "0" + hour; | |
} else { | |
hourtext = "" + hour; | |
} | |
int minute = entity.getminute(); | |
if(minute < 10) { | |
minutetext = "0" + minute; | |
} else { | |
minutetext = "" + minute; | |
} | |
int second = entity.getsecond(); | |
if(second < 10) { | |
secondtext = "0" + second; | |
} else { | |
secondtext = "" + second; | |
} | |
listid = entity.getId(); | |
roomtext2.setText(year + ":" + monthtext + ":" + daytext + ":" + hourtext + ":" + minutetext + ":" + secondtext); | |
roomtext3.setText(listpage + "/" + listmax); | |
roomtextmax.setText("データ保存数:" + listmax); | |
} else { | |
roomtext3.setText("データ無し"); | |
roomtext2.setText(""); | |
roomtextmax.setText("データ保存数:0"); | |
roomtextdebug.setText(""); | |
} | |
} | |
}); | |
} | |
private void writedb() { | |
ExecutorService executor = Executors.newSingleThreadExecutor(); | |
executor.execute(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
Calendar c = Calendar.getInstance(); | |
int year = c.get(Calendar.YEAR); | |
int month = c.get(Calendar.MONTH) + 1; | |
int day = c.get(Calendar.DATE); | |
int hour = c.get(Calendar.HOUR); | |
int minute = c.get(Calendar.MINUTE); | |
int second = c.get(Calendar.SECOND); | |
RoomTimeDatabase db = Room.databaseBuilder(getApplicationContext(), | |
RoomTimeDatabase.class, "roomtime").build(); | |
RoomTimeDao dao = db.roomtimedao(); | |
RoomEntity entity = new RoomEntity(year, month, day, hour, minute, second); | |
dao.insert(entity); | |
roomtextdebug.setText("書き込み完了"); | |
listpage = listmax + 1; | |
loaddb(); | |
} catch(Exception e) { | |
roomtextdebug.setText("エラー"); | |
} | |
} | |
}); | |
} | |
private void deletedb() { | |
ExecutorService executor = Executors.newSingleThreadExecutor(); | |
executor.execute(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
RoomTimeDatabase db = Room.databaseBuilder(getApplicationContext(), | |
RoomTimeDatabase.class, "roomtime").build(); | |
RoomTimeDao dao = db.roomtimedao(); | |
dao.delete(listid); | |
listpage = 1; | |
loaddb(); | |
} catch (Exception e) { | |
roomtextdebug.setText("エラー"); | |
} | |
} | |
}); | |
} | |
private void deletealldb() { | |
ExecutorService executor = Executors.newSingleThreadExecutor(); | |
executor.execute(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
RoomTimeDatabase db = Room.databaseBuilder(getApplicationContext(), | |
RoomTimeDatabase.class, "roomtime").build(); | |
RoomTimeDao dao = db.roomtimedao(); | |
dao.deleteall(); | |
listpage = 1; | |
loaddb(); | |
} catch (Exception e) { | |
roomtextdebug.setText("エラー"); | |
} | |
} | |
}); | |
} | |
@Override | |
public void onClick(View v) { | |
switch(v.getId()) { | |
case(R.id.roombtnadd): | |
writedb(); | |
break; | |
case(R.id.roombtnleft): | |
if(listpage > 1) { | |
listpage -= 1; | |
loaddb(); | |
roomtextdebug.setText(""); | |
} | |
break; | |
case(R.id.roombtnright): | |
if(listpage < listmax) { | |
listpage += 1; | |
loaddb(); | |
roomtextdebug.setText(""); | |
} | |
break; | |
case(R.id.roombtndelete): | |
deletedb(); | |
break; | |
case(R.id.roombtnalldelete): | |
deletealldb(); | |
break; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uta.utauta.roomdb; | |
import androidx.room.Entity; | |
import androidx.room.PrimaryKey; | |
@Entity(tableName = "roomtime") | |
public class RoomEntity { | |
@PrimaryKey(autoGenerate = true) | |
public int id; | |
public int year; | |
public int month; | |
public int day; | |
public int hour; | |
public int minute; | |
public int second; | |
public RoomEntity(int year, int month, int day, int hour, int minute, int second) { | |
this.year = year; | |
this.month = month; | |
this.day = day; | |
this.hour = hour; | |
this.minute = minute; | |
this.second = second; | |
} | |
public int getId() { | |
return this.id; | |
} | |
public int getyear() { | |
return this.year; | |
} | |
public int getmonth() { | |
return this.month; | |
} | |
public int getday() { | |
return this.day; | |
} | |
public int gethour() { | |
return this.hour; | |
} | |
public int getminute() { | |
return this.minute; | |
} | |
public int getsecond() { | |
return this.second; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uta.utauta.roomdb; | |
import androidx.room.Dao; | |
import androidx.room.Insert; | |
import androidx.room.Query; | |
import java.util.List; | |
@Dao | |
public interface RoomTimeDao { | |
@Query("SELECT * FROM roomtime") | |
List<RoomEntity> getall(); | |
@Query("DELETE FROM roomtime") | |
void deleteall(); | |
@Query("DELETE FROM roomtime WHERE id = :x") | |
void delete(int x); | |
@Insert | |
void insert(RoomEntity roomentity); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uta.utauta.roomdb; | |
import androidx.room.Database; | |
import androidx.room.RoomDatabase; | |
@Database(entities = {RoomEntity.class}, version = 1, exportSchema = false) | |
public abstract class RoomTimeDatabase extends RoomDatabase { | |
public abstract RoomTimeDao roomtimedao(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment