-1

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.

Community
  • 1
  • 1
JimFitz
  • 51
  • 3

3 Answers3

0

In your Item class , can see the variables to be of type static which could be a possible reason. Try removing static keyword as in this scenario, these variables/properties values are dependent on the object instead of being common for all objects

public class Item {
    private int idInt;
    private String description;
    private Image image;

    public Item(int idInt, String description,Image image) {
         this.idInt = idInt;
         this.description = description;
         this.image = image;
    }

Hope this solves your problem..

Shubhi224
  • 104
  • 6
-1

I've run into this issue before. I was unable to find a solution at the time, so I ended up taking a different approach, which was to use an SQLite database to store information about my items in local storage. I am not sure this helps you, but it is a viable alternative to storing data on regular files.

frank34443
  • 29
  • 6
  • 1
    This has nothing to do with their storage. They clearly have `static` variables instead of instance variables in their `Item` class. – Savior Apr 08 '16 at 17:29
  • Thanks for that. I did actually consider this route but I realise now that it is unnecessary. – JimFitz Apr 10 '16 at 12:45
-1

Using static for the fields in your Item class causes them to belong to the class, not to the instance of the class. Each time you create a new Item, you're resetting the class fields to the new values. Your Item objects don't have any instance-specific data delared.

brycem
  • 593
  • 3
  • 9