UserAgr UserAgr - 2 months ago 6
Android Question

Add only last item in arraylist twice

I need to add all items in the arraylist

Logcat:

E/BUSINESS_STR: fuflfilled
E/itemsaArrayList: [com.model.CommonListItems@307c24ff, com.model.CommonListItems@307c24ff]


Code:

if(BUSINESS_STR != null){

Log.e("BUSINESS_STR", "" + BUSINESS_STR);

CommonListItems commonListItems = new CommonListItems();


for(int i = 0; i <= 1 ; i++) {
commonListItems.setName("My Business");
commonListItems.setName("Search Business");

commonListItems.setImage(String.valueOf(R.drawable.business_icon));
commonListItems.setImage(String.valueOf(R.drawable.search_business_icon));

itemsaArrayList.add(commonListItems);
}

tvTitle.setText("Business");
}


Finally it is adding the last item
"Search Business"
twice. I need to add
"My Business"
first and then
"Search Business"
.

Answer

Remove for loop:

commonListItems.setName("My Business");
commonListItems.setImage(String.valueOf(R.drawable.business_icon));
itemsaArrayList.add(commonListItems);

commonListItems = new CommonListItems();

commonListItems.setName("Search Business");
commonListItems.setImage(String.valueOf(R.drawable.search_business_icon));
itemsaArrayList.add(commonListItems);