When Fragment starts at 16, its EditText should load data from the database. Further, after closing the Fragment, the entered data must be entered into the database. At the subsequent discoveries of this Fragment, the same data should be loaded again, but with the changes made before. Question: how to do this? Well, at least in 4 EditText for a start, otherwise it will write a lot.
Here is the DBHelper class:
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper{ public static final int DATABASE_VERSION = 1; public static final String DATABASE_NAME = "FirstDatabase"; public static final String TABLE_RINGS = "rings"; public static final String KEY_ID = "_id"; public static final String KEY_START = "start"; public static final String KEY_END = "end"; public DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table " + TABLE_RINGS + "(" + KEY_ID + " integer primary key," + KEY_START + " text," + KEY_END + " text" + ")"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table if exists " + TABLE_RINGS); onCreate(db); } } Here is a fragment (gives an error):
import android.app.Fragment; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.TextView; public class FragmentRings extends Fragment { DBHelper dbHelper; SQLiteDatabase database; @Override public void onStart() { super.onStart(); dbHelper = new DBHelper(getActivity()); EditText one = (EditText) getView().findViewById(R.id.oneurok); EditText oneee = (EditText) getView().findViewById(R.id.editText2); EditText two = (EditText) getView().findViewById(R.id.twourok); EditText twooo = (EditText) getView().findViewById(R.id.editText3); database = dbHelper.getWritableDatabase(); Cursor userCursor = database.query(DBHelper.TABLE_RINGS, null, null, null, null, null, null); if (userCursor.moveToFirst()) { while (!userCursor.isAfterLast()) { String data = userCursor.getString(userCursor.getColumnIndex(DBHelper.KEY_START)); String data1 = userCursor.getString(userCursor.getColumnIndex(DBHelper.KEY_END)); one.setText(data); oneee.setText(data1); userCursor.moveToNext(); String data2 = userCursor.getString(userCursor.getColumnIndex(DBHelper.KEY_START)); String data3 = userCursor.getString(userCursor.getColumnIndex(DBHelper.KEY_END)); two.setText(data); twooo.setText(data1); } } userCursor.close(); } @Override public void onStop() { database = dbHelper.getWritableDatabase(); ContentValues contentValues = new ContentValues(); EditText one = (EditText) getView().findViewById(R.id.oneurok); EditText oneee = (EditText) getView().findViewById(R.id.editText2); EditText two = (EditText) getView().findViewById(R.id.twourok); EditText twooo = (EditText) getView().findViewById(R.id.editText3); contentValues.put(DBHelper.KEY_START, one.getText().toString()); contentValues.put(DBHelper.KEY_END, oneee.getText().toString()); contentValues.put(DBHelper.KEY_START, two.getText().toString()); contentValues.put(DBHelper.KEY_END, twooo.getText().toString()); database.insert(DBHelper.TABLE_RINGS, null, contentValues); super.onStop(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View RingsView = inflater.inflate(R.layout.fragment_rings, container, false); return RingsView; } } Error log when opening a fragment on the emulator:
08-01 16:49:24.686 3339-3339/com.churkin.myproject E/AndroidRuntime: FATAL EXCEPTION: main Process: com.churkin.myproject, PID: 3339 android.database.CursorIndexOutOfBoundsException: Index 7 requested, with a size of 7 at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426) at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) at com.churkin.myproject.FragmentRings.onStart(FragmentRings.java:70) at android.app.Fragment.performStart(Fragment.java:2077) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:922) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.app.BackStackRecord.run(BackStackRecord.java:834) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
onStartto look inonStartandonStopview every time to work with them. Do it once inonCreateViewand save the foundonCreateViewto the class fields. - temq