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) 
  • So what exactly is the problem? - Android Android
  • @AndroidAndroid I do not know how to make the correct implementation in the fragment. The code I gave in the fragment gives an error - Alex777Ch
  • @AndroidAndroid now attach log - Alex777Ch
  • one
    There is no onStart to look in onStart and onStop view every time to work with them. Do it once in onCreateView and save the found onCreateView to the class fields. - temq
  • one
    Well, of course you can not, you need to use the view that is fixed. - temq

1 answer 1

That's the way to work with the cursor in your case.

 Cursor userCursor = database.query(DBHelper.TABLE_RINGS, null, null, null, null, null, null); if (userCursor != null ) { if (userCursor.moveToFirst()) { do { // читаем только одну запись, никаких переходов дальше String data = userCursor.getString(userCursor.getColumnIndex(DBHelper.KEY_START)); String data1 = userCursor.getString(userCursor.getColumnIndex(DBHelper.KEY_END)); // заносим данные в UI // ... } while (userCursor.moveToNext()); } userCursor.close(); } 

If you have only two pairs of controls, read only the first two entries and the break from the loop.

  • Just the usual while (userCursor.moveToNext()) {} without additional checks. So it will be shorter and more beautiful. - temq
  • one
    This is a standard code snippet, the author will understand how it works, even if it shortens. - Eugene Krivenja
  • @EugeneKrivenja Well, in two, then I consider the EditView and bring it in, and how to enter it in others? I have a lot of them, it is necessary that everything is entered into all 16 ... And there is only 2 key ... Maybe some additional check can be done, if there are no lines further than the cursor, then do nothing, and if there is, then enter further ... And besides, it is not possible to correctly enter the data into the database from more than two EditViews - Alex777Ch
  • @AlexanderChurkin, what's the problem? At each iteration of the loop, you will have a pair of values ​​from the database row, how many rows in the table, so many pairs of data. How do you push them through the UI, decide for yourself. - Eugene Krivenja
  • @EugeneKrivenja aaa, I understand, thank you) You opened your eyes to me as it were) In many moments it would not seem very simple to get intuitive sometimes, then I analyze ... And with SQLite, I probably needed someone to explain "on the fingers" )) - Alex777Ch