アプリを消しても数値が保存してあるようにしたいのですがうまくいきません。
データベースについてのサイトをいくつか見たのですがよく理解できませんでした。
現時点でのコードを載せるのでどこにどうコードを追加すればうまくいくか教えてください。
文字数制限のせいでインデント汚くなってるのとXMLのコード省略してます。
activity_create_memo.xml
<EditText
<EditText
<LinearLayout
<Button
<Button
<Button
<TextView
<Button
</android.support.constraint.ConstraintLayout>
activity_list.xml
<Button android:id="@+id/newButton" android:layout_width="155dp" android:layout_height="50dp" android:layout_marginTop="1dp" android:layout_weight="1" android:onClick="onClick" android:text="新規作成する" app:layout_constraintTop_toTopOf="parent" tools:layout_editor_absoluteX="16dp" />
<ListView android:id="@+id/memoList" android:layout_width="351dp" android:layout_height="474dp" android:layout_weight="9" tools:layout_editor_absoluteX="17dp" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/newButton" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:onClick="onClick2"
android:text="戻る"
app:layout_constraintLeft_toRightOf="@+id/newButton"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteY="1dp" />
</android.support.constraint.ConstraintLayout>
ListActivity.java
public class ListActivity extends AppCompatActivity {
MemoOpenHelper helper = null;
public int global;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
if (helper == null) {
helper = new MemoOpenHelper(ListActivity.this);
}
final ArrayList<HashMap<String, String>> memoList = new ArrayList<>();
SQLiteDatabase db = helper.getWritableDatabase();
try {
Cursor c = db.rawQuery("select uuid, body from MEMO_TABLE order by id", null);
boolean next = c.moveToFirst();
while (next) {
HashMap<String, String> data = new HashMap<>();
String uuid = c.getString(0);
String body = c.getString(1);
if (body.length() > 10) {
body = body.substring(0, 11) + "...";
}
data.put("body", body);
data.put("id", uuid);
memoList.add(data);
next = c.moveToNext();
}
} finally {
db.close();
}
final SimpleAdapter simpleAdapter = new SimpleAdapter(this,
memoList,
android.R.layout.simple_list_item_2,
new String[]{"body", "id"},
new int[]{android.R.id.text1, android.R.id.text2}
);
ListView listView = (ListView) findViewById(R.id.memoList);
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(ListActivity.this, com.example.cs1.mymukou3.CreateMemoActivity.class);
TwoLineListItem two = (TwoLineListItem) view;
TextView idTextView = (TextView) two.getText2();
String isStr = (String) idTextView.getText();
intent.putExtra("id", isStr);
startActivity(intent);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
TwoLineListItem two = (TwoLineListItem) view;
TextView idTextView = (TextView) two.getText2();
String idStr = (String) idTextView.getText();
SQLiteDatabase db = helper.getWritableDatabase();
try {
db.execSQL("DELETE FROM MEMO_TABLE WHERE uuid = '" + idStr + "'");
} finally {
db.close();
}
memoList.remove(position);
simpleAdapter.notifyDataSetChanged();
return true;
}
});
}
public void onClick(View v) {
Intent intent = new Intent(ListActivity.this, com.example.cs1.mymukou3.CreateMemoActivity.class);
intent.putExtra("id", "");
startActivity(intent);
}
public void onClick2(View a) {
Intent intent = new Intent(ListActivity.this, MainActivity.class);
startActivity(intent);
}
}
CreateActivity.java
public class CreateMemoActivity extends AppCompatActivity {
MemoOpenHelper helper = null;
boolean newFlag = false;
String id = "";
public SharedPreferences dataStore;
public SharedPreferences dataStore2;
public EditText editText;
public TextView textWrite, textRead;
public int c =0;
public int d=0;
public int global=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_memo);
dataStore = getSharedPreferences("DataStore", MODE_PRIVATE);
dataStore2 = getSharedPreferences("DataStore", MODE_PRIVATE);
editText = (EditText) findViewById(R.id.edit_text);
textRead = (TextView)findViewById(R.id.text_read);
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = data.edit();
editor.putInt("LevelSave", 1);
editor.apply();*/
if(helper == null){
helper = new MemoOpenHelper(CreateMemoActivity.this);
}
Intent intent = this.getIntent();
id = intent.getStringExtra("id");
if(id.equals("")){
newFlag = true;
}else{
SQLiteDatabase db = helper.getWritableDatabase();
try {
Cursor c = db.rawQuery("select body from MEMO_TABLE where uuid = '"+ id +"'", null);
boolean next = c.moveToFirst();
while (next) {
String dispBody = c.getString(0);
EditText body = (EditText)findViewById(R.id.body);
body.setText(dispBody, TextView.BufferType.NORMAL);
next = c.moveToNext();
}
} finally {
db.close();
}
}
Button registerButton = (Button) findViewById(R.id.register);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText body = (EditText)findViewById(R.id.body);
String bodyStr=body.getText().toString();
SQLiteDatabase db = helper.getWritableDatabase();
try {
if(newFlag){
id = UUID.randomUUID().toString();
db.execSQL("insert into MEMO_TABLE(uuid, body) VALUES('"+ id +"', '"+ bodyStr +"')");
// db.execSQL("insert into MEMO_TABLE(uuid, edit_text) VALUES('"+ id +"', '"+ bodyStr +"')");
}else{
// UPDATE
db.execSQL("update MEMO_TABLE set body = '"+ bodyStr +"' where uuid = '"+id+"'");
}
}finally {
db.close();
}
Intent intent = new Intent(CreateMemoActivity.this, com.example.cs1.mymukou3.ListActivity.class);
startActivity(intent);
}
});
Button backButton = (Button) findViewById(R.id.back);
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CreateMemoActivity.this, com.example.cs1.mymukou3.ListActivity.class);
startActivity(intent);
}
});
}
public void onClick(View v) {
Editable getText = editText.getText();
int int_text = Integer.parseInt(getText.toString());
SharedPreferences.Editor editor = dataStore.edit();
editor.putInt("input", int_text);
editor.apply();
}
public void onClick3(View v) {
int b = dataStore.getInt("input",0);
c=b;
d=c+d;
SharedPreferences.Editor editor = dataStore2.edit();
editor.putInt("d", d);
editor.apply();
textRead.setText(String.valueOf(d));
}
}
MemoOpenHelper.java
public class MemoOpenHelper extends SQLiteOpenHelper {
static final private String DBName = "MEMO_DB";
static final private int VERSION = 1;
public MemoOpenHelper(Context context){
super(context, DBName, null, VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE MEMO_TABLE (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"uuid TEXT, " +
"body TEXT)");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS MEMO_TABLE");
onCreate(db);
}
}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。