1

I have written following lines of code and seems everything is OK, however when I see result its not correct.

Here, I defined a LinkedList and adding an object into it.

private LinkedList<KategoriRecipeList> createListObjects() {

        LinkedList<KategoriRecipeList> list = new LinkedList<KategoriRecipeList>();
        KategoriRecipeList kategori = new KategoriRecipeList();

        for(int i=0; i<resepi.getResepiId().size(); i++) {
            kategori.setKategoriName(kategoriName);
            kategori.setResepiId(resepi.getResepiId().get(i));
            kategori.setResepiName(resepi.getResepiName().get(i).trim().replaceAll("'", ""));
            kategori.setImgLogoUri(resepi.getResepiImageURL().get(i));
            kategori.setImgMoreUri(bitmapUrls.get(i));

            Log.i("WWWWWWW", "Recipe '" + resepi.getResepiName().get(i) + "' added to list");

            list.add(kategori);
        }

        return list;
    }

When I see Log file the result is like this:

09-09 03:05:29.272: I/WWWWWWW(1250): Recipe '"Chicken Cordon Bleu"' added to list

09-09 03:05:29.272: I/WWWWWWW(1250): Recipe '"Sat-Bag"' added to list

09-09 03:05:29.272: I/WWWWWWW(1250): Recipe 'Ayam Adobo ' added to list

09-09 03:05:29.272: I/WWWWWWW(1250): Recipe 'Ayam Ber’Crumble’ & ‘Fennel’ direneh Air Roselle' added to list

09-09 03:05:29.272: I/WWWWWWW(1250): Recipe 'Ayam Dua Cara' added to list

I have another method which invokes above method:

private void storeListIntoDatabase() {
        Log.i(TAG, "Try to insert data into database.");

        LinkedList<KategoriRecipeList> listOfObjects = createListObjects();
        if(listOfObjects == null  ||  listOfObjects.size() <= 0)
            return;

        for(int i=0;  i<listOfObjects.size(); i++)
            Log.i("KKKKKKKK", "Recipe '" + listOfObjects.get(i).getResepiName() + "' added to list");
    }

Result in log file is like this:

09-09 03:05:29.297: I/KKKKKKKK(1250): Recipe 'Tongkeng Ayam Goreng dan Nasi Kari Leher Ayam' added to list

09-09 03:05:29.297: I/KKKKKKKK(1250): Recipe 'Tongkeng Ayam Goreng dan Nasi Kari Leher Ayam' added to list

09-09 03:05:29.297: I/KKKKKKKK(1250): Recipe 'Tongkeng Ayam Goreng dan Nasi Kari Leher Ayam' added to list

09-09 03:05:29.297: I/KKKKKKKK(1250): Recipe 'Tongkeng Ayam Goreng dan Nasi Kari Leher Ayam' added to list

09-09 03:05:29.297: I/KKKKKKKK(1250): Recipe 'Tongkeng Ayam Goreng dan Nasi Kari Leher Ayam' added to list

As you can see results are different but i expect result should be same. Where is my problem? Thanks

1
  • Isn't the result already the same? Commented Sep 8, 2012 at 19:22

2 Answers 2

3

You need to create new instance on each loop otherwise same instance will be updated. Lists maintains reference to object, they don't contain copy of the object.

for(int i=0; i<resepi.getResepiId().size(); i++) {
    KategoriRecipeList kategori = new KategoriRecipeList();
    kategori.setKategoriName(kategoriName);
    kategori.setResepiId(resepi.getResepiId().get(i));
    ....
}
Sign up to request clarification or add additional context in comments.

Comments

2

this line

KategoriRecipeList kategori = new KategoriRecipeList();

needs to be inside the for loop you have to create a new object every time

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.