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);
}
}