B.Cakir
B.Cakir

Reputation: 607

How to loop through a custom list?

I have this peace of code and want to know how to make it more elegant by looping through it. Tried to put the list and adapters in an object array, but I couldn't call .size() with that.

So from this:

int breakfastIndex = //get index.
if(breakfastIndex != -1 && breakfastFoodList.size() > breakfastIndex) {
    breakfastFoodList.remove(breakfastIndex);
    breakfastAdapter.notifyItemRemoved(breakfastIndex);
}

int lunchIndex = intent.getIntExtra("position", -1);
if(lunchIndex != -1 && lunchFoodList.size() > lunchIndex) {
    lunchFoodList.remove(lunchIndex);
    lunchAdapter.notifyItemRemoved(lunchIndex);
}

int dinnerIndex = intent.getIntExtra("position", -1);
if(dinnerIndex != -1 && dinnerFoodList.size() > dinnerIndex) {
    dinnerFoodList.remove(dinnerIndex);
    dinnerAdapter.notifyItemRemoved(dinnerIndex);
}

int snackIndex = intent.getIntExtra("position", -1);
if(snackIndex != -1 && snackFoodList.size() > snackIndex) {
    snackFoodList.remove(snackIndex);
    snackAdapter.notifyItemRemoved(snackIndex);
}

To this:

for(int i = 0; i < 4; i++) {
    int index = //get index.
    if(index != -1 && foodList[i].size() > index) {
        foodList[i].remove(index);
        adapter[i].notifyItemRemoved(index);
    }
}

Upvotes: 2

Views: 216

Answers (2)

B.Cakir
B.Cakir

Reputation: 607

Some people pointed out that my code contained a bug, which in hindsight seems to be the reason why my code looked "ugly" to me.

I settled on using a simple switch.

int index = intent.getIntExtra("position", -1);
String mealType = intent.getStringExtra("mealType");

switch(mealType) {
    case "BREAKFAST":
        if(index != -1 && breakfastFoodList.size() > index) {
            breakfastFoodList.remove(index);
            breakfastAdapter.notifyItemRemoved(index);
        }
    break;
    case "LUNCH":
        if(index != -1 && lunchFoodList.size() > index) {
            lunchFoodList.remove(index);
            lunchAdapter.notifyItemRemoved(index);
        }
        break;
    case "DINNER":
        if(index != -1 && dinnerFoodList.size() > index) {
            dinnerFoodList.remove(index);
            dinnerAdapter.notifyItemRemoved(index);
        }
        break;
   case "SNACK":
        if(index != -1 && snackFoodList.size() > index) {
            snackFoodList.remove(index);
            snackAdapter.notifyItemRemoved(index);
        }
        break;
}

Upvotes: 1

OneCricketeer
OneCricketeer

Reputation: 191681

You can make two arrays

List[] lists = {breakfastFoodList,...};
ArrayAdapter[] adapters = {breakfastAdapter,...};

Then, that loop you wrote looks fine.

However, if you're trying to remove all meals at once, I feel like you should store one list, then derive the rest

For example,

public class DailyMeal {
    Meal breakfast, lunch, dinner, snack;
}

Then, for your lists

// In the Activity 
private List<DailyMeal> meals;
public List<Meal> getBreakfasts();  // return list of all  breakfasts
public List<Meal> getLunches();
// etc.

When you remove a DailyMeal object from the meals list, you then automatically are removing each of the Meal objects for that "day"

Upvotes: 2

Related Questions