Alaa AbuZarifa
Alaa AbuZarifa

Reputation: 1249

How to handle (else if) for dynamic conditions

What if I want to select a certain item/s from a fixed list of hobbies for examples. Let's assume I chose (Swimming) and (D&D) from the whole list.

inside I want to check what did he chose to something..! and I would do it like this:

for (String hobby : selectedHobbiesList)
{

  if (hobby.equals("Swimming"))
  {
    return 2;
  }
  else if (hobby.equals("D&D"))
  {
    return 1;
  }
  else
  {
    return 0;
  }
}

I want to set number of conditions dynamically, I know I can just use else if for all the hoppies, but in my case I want condition as many hobbies selected..!

is that even possible or is there a work around.!


UPDATE

I think I wasn't successful in explaining my point, sorry about that..! I asked the question with a dummy example because I'm having a problem with handling (else if).. but I don't know how to apply the suggested answer with my real problem..! And I'm going to explain it more clearly.


I'm trying to reorder an arraylist items in a recyclerview in terms of hobbies for example. And I gave example of two hobbies as in the previous example. but in my case the hobbies are NOT preset..! they can be up to 8 or none! so I tried to make "else if" dynamic like that:

     // inside the activity class

      if (homeData.size() > 0) {

        Collections.sort(homeData,
            Collections.reverseOrder(new SortedListAdapterCallback<HomeItemModel>(adapter) {
              @Override
              public int compare(HomeItemModel o1, HomeItemModel o2) {

                if (Hawk.contains("Hobbies")) {

                  ArrayList<String> hobbiesList = Hawk.get("Hobbies");

                  for (String hobbyItem: hobbiesList ){

                    if (o1.getCategory().equals("Swimming") && item.equals("Swimming")) {
                    weight1 = 2;

                    } else if (o1.getCategory().equals("D&D") && item.equals("D&D")) {
                    weight1 = 1;

                   // I know must define all the 8 hobbies but I don't wanna make the code to long..
                    } else { 
                    weight1 = 0;
                    }

                    if (o2.getCategory().equals("Swimming") && item.equals("Swimming")) {
                    weight2 = 2;

                    } else if (o2.getCategory().equals("D&D") && item.equals("D&D")) {
                    weight2 = 1;

                    } else {
                    weight2 = 0;
                    }

                  }




                  if (weight1 == weight2) {
                    return 0;
                  } else if (weight1 > weight2) {
                    return 1;
                  } else {
                    return -1;
                  }     

                }
                return 0;

              }

              @Override
              public boolean areContentsTheSame(HomeItemModel oldItem, HomeItemModel newItem) {
                return false;
              }

              @Override
              public boolean areItemsTheSame(HomeItemModel item1, HomeItemModel item2) {
                return false;
              }
            }));

      }
      adapter.notifyDataSetChanged();

      // code..

but this isn't working, it only recognises one of the conditions, so if the hobbies arraylist contains {"Swimming","D&D"} it only sorts the first one but before When I did it manually it was sort both!

Upvotes: 0

Views: 558

Answers (2)

With HashMap:

HashMap map = new HashMap<String, Integer>(){
        {
            put("Other", 3);
            put("Swimming", 2);
            put("D&D", 1);
        }
    };



    for (String hobby : selectedHobbiesList) {
        if(map.containsKey(hobby)){
            return map.get(hobby);
        }else{
            return 0;
        }
    }

UPDATE:

Why not set the weight in your HomeItemModel?

class HomeItemModel{
    private int mWeight;
    private String mCat;

    HomeItemModel(String cat, int weight){
        mWeight = weight;
        mCat = cat;
    }
    public int getWeight(){
        return mWeight;
    }
    public String getCategory(){
        return mCat;
    }
    /*More code*/
}

Comparator:

        @Override
        public int compare(HomeItemModel o1, HomeItemModel o2){
            //DESC order
            if(o1.getWeight() == o2.getWeight()){
                return 0;
            }else if(o1.getWeight() < o2.getWeight()){
                return 1;
            }else{
                return -1;
            }
        }

Add data:

homeData.add(new HomeItemModel("Swimming", 1));
homeData.add(new HomeItemModel("D&D", 2));

Upvotes: 1

Sergey Emeliyanov
Sergey Emeliyanov

Reputation: 6961

As I understood, you want to learn what were the choices. I'd use a simple switch-case statement:

for(String hobby : selectedHobbiesList) {
    switch(hobby) {
        case "Swimming":
        // Do something here
        break;
        case "D&D":
        // Do something here 
        break;
        ...
    }

Upvotes: 0

Related Questions