'Auto Increment increments all values of the serial number variable when new record is added in List of JSON JACKSON
I am new to JSON, I have implemented a JSON Jackson code from this Github Link
I've edited the code and added a integer variable sno which auto increments. Below is my Car class
public class Car {
int year, sno;
String make;
@Override
public String toString() {
return "Car{" +
"sno =" + sno +
"year=" + year +
", make='" + make + '\'' +
'}'; }
public Car(int sno, int year, String make) {
this.sno = sno;
this.year = year;
this.make = make;
}
public Car() {
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
}
In my MainActivity class i've coded as follows:
int intSno;
sno= (TextView) findViewById(R.id.sno);
sno.setText(String.valueOf(intSno));
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DataService dataService = new DataService(view.getContext());
list = dataService.readList("cars.txt");
Car c1 = new Car(intSno, 2022, "Car Make");
list.getCarList().add(c1);
intSno = intSno + 1; // this line increments the serial number
list.getCarList().add(donor);
dataService.writeList(list, "cars.txt");
}
});
The problem I face is the code replaces all the sno variables in the txtfile, as it reads from the list, when I exit the app and run it again. ( the sno increments correctly when I stay in the app and save the data by pressing the save button)
How to access the last sno variable in the list, (go to the EndOfFile of "Cars.txt") and increment the integer variable sno from the next record onwards in my code?
Thanks in Advance!!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
