I'm new to Java and Android. Part of an app I'm writing reads item data from a file and populates an ArrayList of items prior to using it to populate a ListView.
From main.java:-
public class MainActivity extends AppCompatActivity {
public static ArrayList<Item> items;
.
.
.
private void ReadItemsFile() throws IOException {
File itemsFile = new File(itemsFilenameString);
items = new ArrayList<Item>();
try (BufferedReader itemsBufferedReader = new BufferedReader(new FileReader(itemsFile))) {
for (String line; (line = itemsBufferedReader.readLine()) != null; ) {
String[] lineStrings = line.split("\t|\n", 2);
int itemNo = Integer.parseInt(lineStrings[0]);
items.add(new Item(itemNo, lineStrings[1]));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
From Items.java:-
public class Item {
private static int idInt;
private static String description;
private static Image image;
public Item(int idInt, String description) {
this.idInt = idInt;
this.description = description;
this.image = image;
}
}
but when I run this I find that the ArrayList (and ListView) is full of items that all are the same as the last one read from the file. I've tried debugging this and found that all the items in the ArrayList are changed to the last one added after the line:-
items.add(new Item(itemNo, lineStrings[1]));
Please can someone explain why this is and how to fix it?
I've previously checked Creating an ArrayList of Objects on this site and found my method of ArrayList population to be the same as was suggested.