0
android開発でスワイプとデーターベースの保存を同時に実装する方法
こんにちはです。javaというよりandroid開発になってしまうのですが、スワイプ機能とデーターベースへの保存を同時に実装する方法がわかりません。一応スワイプ機能とデーターベースへの保存はそれぞれ独立には実装する事はできるのですが、あわせるとうまくいきません。どうしてでしょうか?アドバイスいただけるとうれしいです。
現象は、ページが3つあり、スワイプして2ページ目に「商品名」と「消費期限」を入力する項目があります。商品名と消費期限を入力して「保存」ボタンを押すと、商品名と消費期限が関連付けられてデーターベースに保存されるはずなのですが、なぜだかこのとき「アプリケーションが繰り返し終了しています」と表示されます。
そのコードは次のようになります。
「DatabaseHelper.java」
public class DatabaseHelper extends SQLiteOpenHelper {
static final private String DBNAME="sample.sqlite";
static final private int VERSION=1;
//コンストラクター
public DatabaseHelper(Context context){
super(context, DBNAME,null,VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE data_of_expiration_date("+
"pn TEXT PRIMARY KEY,ed TEXT)");
db.execSQL("INSERT INTO data_of_expiration_date(pn,ed)"+
"VALUES('肉','D+3')");
db.execSQL("INSERT INTO data_of_expiration_date(pn,ed)"+
"VALUES('卵','D+5')");
db.execSQL("INSERT INTO data_of_expiration_date(pn,ed)"+
"VALUES('マヨネーズ','D+10')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int old_v, int new_v) {
db.execSQL("DROP TABLE IF EXISTS data_of_expiration_date");
onCreate(db);
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
}
}
「MainActivity.java」
public class MainActivity extends AppCompatActivity {
private DatabaseHelper helper=null;
private EditText txtName=null;
private EditText txtDate=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper=new DatabaseHelper(this);
txtName=(EditText) findViewById(R.id.txtName_id);
txtDate=(EditText) findViewById(R.id.txtDate_id);
ViewPager mViewPager = (ViewPager)findViewById(R.id.viewpager);
PagerAdapter mPagerAdapter = new MyPagerAdapter();
mViewPager.setAdapter(mPagerAdapter);
}
//[保存]
public void onSave(View view){
SQLiteDatabase db =helper.getWritableDatabase();
try{
ContentValues cv=new ContentValues();
cv.put("pn",txtName.getText().toString());
cv.put("ed",txtDate.getText().toString());
db.insertWithOnConflict("data_of_expiration_date",null,cv,SQLiteDatabase.CONFLICT_REPLACE);
Toast.makeText(this,"データの登録に成功しました",
Toast.LENGTH_SHORT).show();
}finally{
db.close();
}
}
//[検索]
public void onSearch(View view){
SQLiteDatabase db=helper.getReadableDatabase();
Cursor cs=null;
try{
String[] cols={"pn","ed"};
String[] params={txtName.getText().toString()};
cs=db.query("data_of_expiration_date",cols,"pn=?",params,null,null,null,null);
if(cs.moveToFirst()){
txtDate.setText(cs.getString(1));
}else{
Toast.makeText(this,"データがありません",
Toast.LENGTH_SHORT).show();
}
}finally{
cs.close();
db.close();
}
}
private class MyPagerAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, int position) {
// レイアウトファイル名を配列で指定します。
int[] pages = {R.layout.page1, R.layout.page2, R.layout.page3};
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout ;
layout = inflater.inflate(pages[position], null);
((ViewPager) container).addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager)container).removeView((View)object);
}
@Override
public int getCount() {
// ページ数を返します。
return 3;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
}}
「activity_main_xml」
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.wixsite.tobirayt.myapplication.MainActivity">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/viewpager"
/>
</android.support.constraint.ConstraintLayout>
「page1.xml」
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
「page2.xml」
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="64dp"
android:onClick="onSearch"
android:text="@string/search_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.535" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginLeft="36dp"
android:onClick="onSave"
android:text="@string/save_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.535" />
<MultiAutoCompleteTextView
android:id="@+id/txtName_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="@string/product_name_text"
app:layout_constraintBottom_toTopOf="@+id/txtDate_id"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<MultiAutoCompleteTextView
android:id="@+id/txtDate_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="144dp"
android:layout_marginTop="8dp"
android:text="@string/expiration_date_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
「page3.xml」
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
「string.xml」
<resources>
<string name="app_name">My Application</string>
<string name="save_text">保存</string>
<string name="search_text">検索</string>
<string name="product_name_text">商品名</string>
<string name="expiration_date_text">使用期限</string>
</resources>
です。「商品名」「消費期限」の場所に文字を入力して「保存」をクリックすると、「アプリケーションが繰り返し閉じています」と表示されます。全く原因も対処法もわからずお手上げ状態です。
よろしくお願いします。