0

My app is loading data from a SQLite database and loading them in an EditText inside a Listview. Whenever I change the value of the edit text and scroll down the list the EditText goes back to the original value that it got from the database. Is there a way to stop this from happening. I’ve looked around online for an answer but haven’t been able to find one. Any help would be greatly appreciated.

    public class ExerciseAdapter extends CursorAdapter {

    Context context;

    public ExerciseAdapter(Context context, Cursor c) {
        super(context, c, 0);
        this.context = context;
    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        return LayoutInflater.from(context).inflate(R.layout.list_item_exercise, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        int nameCol = cursor.getColumnIndex(WorkoutContract.WorkoutEntry.EXERCISE_NAME);
        String name = cursor.getString(nameCol);
        int weightCol = cursor.getColumnIndex(WorkoutContract.WorkoutEntry.WEIGHT);
        int weightInt = cursor.getInt(weightCol);
        String weight = String.valueOf(weightInt);
        int repCol = cursor.getColumnIndex(WorkoutContract.WorkoutEntry.REPS);
        int repsInt = cursor.getInt(repCol);
        String reps = String.valueOf(repsInt);
        int rpeCol = cursor.getColumnIndex(WorkoutContract.WorkoutEntry.RPE);
        int rpeInt = cursor.getInt(rpeCol);
        String rpe = String.valueOf(rpeInt);

        EditText exerciseName = (EditText) view.findViewById(R.id.exercise_name_table);
        exerciseName.setText(name);
        EditText exerciseWeight  = (EditText) view.findViewById(R.id.exercise_weight_table);
        exerciseWeight.setText(weight);
        EditText exerciseReps = (EditText) view.findViewById(R.id.exercise_reps_table);
        exerciseReps.setText(reps);
        EditText exerciseRpe = (EditText) view.findViewById(R.id.exercise_rpe_table);
        exerciseRpe.setText(rpe);

    }
}

    public class ExerciseActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    private ExerciseAdapter adapter;
    Uri exerciseUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercise);


        ListView listView = findViewById(R.id.list);
        Intent intent = getIntent();
        exerciseUri = intent.getData();
        adapter = new ExerciseAdapter(this, null);
        listView.setAdapter(adapter);
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String[] projections = {
                WorkoutContract.WorkoutEntry._ID,
                WorkoutContract.WorkoutEntry.DAY_ID,
                WorkoutContract.WorkoutEntry.EXERCISE_NAME,
                WorkoutContract.WorkoutEntry.WEIGHT,
                WorkoutContract.WorkoutEntry.REPS,
                WorkoutContract.WorkoutEntry.RPE
        };
        return new CursorLoader(
                this,
                exerciseUri,
                projections,
                null,
                null,
                null
        );
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        adapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        adapter.swapCursor(null);
    }
}
Ali Khaki
  • 1,184
  • 1
  • 13
  • 24
Omar Carino
  • 79
  • 1
  • 2
  • 8
  • @ADM I saw that post while I was trying to research my problem but I didn't really find that answer applicable to my scenario. – Omar Carino Oct 21 '18 at 06:59
  • That means you did not understand the answers there . See all adapter views have Recycling property . Once your view goes out of size of `AdapterView` it recycles and created again when available to show on screen during scroll . You need persist the text during scroll . In my opinion i would avoid to use `EditText` inside `ListView` and look for a some other approach(MayBe take input with a Dialog) . – ADM Oct 21 '18 at 07:04
  • 1
    @ADM I appreciate you explaining that I'll look over it again! Thank you! – Omar Carino Oct 21 '18 at 07:56

1 Answers1

0

You could add an onFocusChanged listener for the EditText and if the focus is not with the EditText update the DB or store the value for subsequent retrieval.

Something along the lines of :-

    et_childname.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if(!b) {
                Child c = (Child) view.getTag();
                c.setName(et_childname.getText().toString());
                ((MainActivity)mActivity).changedChild(c); 
            }
        }
    });
  • where et_childname is an EditText
  • b, the 2nd parameter b is true if the EditText has the focus, else false (hence the check).
  • The tag in this case is the underlying Child object in this case, it is then modified and sent to the activity for handling. This code is within the ListView's adapter.
MikeT
  • 51,415
  • 16
  • 49
  • 68