1

At the moment i have two list one i made it myself and the other one is from Parse.

First List...

public static List<String> getHoraDia(){
    List<String> list = new ArrayList<>();
    list.add("05:00am");
    list.add("05:30am");
    list.add("06:00am");
    list.add("06:30am");
    list.add("07:00am");
    list.add("07:30am");
    list.add("08:00am");
    list.add("08:30am");
    list.add("09:00am");
    list.add("09:30am");
    list.add("10:00am");
    list.add("10:30am");
    list.add("11:00am");
    list.add("11:30am");
    list.add("12:00am");
    list.add("12:30am");
    list.add("1:00pm");
    list.add("1:30pm");
    list.add("2:00pm");
    list.add("2:30pm");
    list.add("3:00pm");
    list.add("3:30pm");
    list.add("4:00pm");
    list.add("4:30pm");
    list.add("5:00pm");
    list.add("5:30pm");

    return  list;
}
   }

The other list has 8:00am and 9:30am there comes my first question can i compare list with different sizes???.

This is what im thinking at the moment ..

int contador = 0;
    for (String horas : horasList) {
        Log.i("HORA","este es horas"+horas);
        for (ParseObject citas : citasTaller) {
            String horaCita = citas.getString("Hora");
            Log.i("HORA","este es horasCita"+horaCita);
            if(horas.equals(horaCita)){

            }
            contador++;
        }

    }

Haven't figure out what to put in the if statement ..

My goal is to accomplish that if the String is in horasList and in citasTaller to be erased from a Spinner. Which i accomplished putting a random position into this...

 horasList.remove(3);
 adapter.notifyDataSetChanged();

So it should go something like

horasList.remove(horaCita)

There's gotta be a better way to do this. In this order of ideas i need to delete from horasList the 8:00am and the 9:30am.

By the way i made this..

horasList = UtilAppFuntions.getHoraDia();

This is the update of the code corresponding to answers... enter image description here

Image about java 7

So i did this but i have a problem @muasif80 once the hour is equal to the string the index is removed and the list is also removed in 1 so the index changes for every value..

ParseQuery<ParseObject> query = ParseQuery.getQuery("CitaTaller");
    qEmp=ParseObject.createWithoutData("Empresa",objIdEmpresa);
    query.whereEqualTo("Empresa",qEmp);
    query.whereEqualTo("fecha", fechaSeleccionada);
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                for (ParseObject obj : objects) {

                    String hora=obj.getString("Hora");

                    citasTaller.add(obj);
                    int listSize = citasTaller.size();

                    for (int i = 0; i < listSize; i++) {
                        Log.i("JOHI", String.valueOf(citasTaller.get(i)));
                    }
                }
                adapter =
                        new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, horasList);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                sphorasdia.setAdapter(adapter);

                for(ParseObject parseObject : citasTaller){
                    String hora = parseObject.getString("Hora");
                    if(hora.equals("05:00am")){
                        horasList.remove(0);
                        adapter.notifyDataSetChanged();
                    }
                    if(hora.equals("05:30am")){
                        horasList.remove(1);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("06:00am")) {
                        horasList.remove(2);
                        adapter.notifyDataSetChanged();


                    }
                    if (hora.equals("6:30am")){
                        horasList.remove(3);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("7:00am")){
                        horasList.remove(4);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("7:30am")){
                        horasList.remove(5);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("8:00am")){
                        horasList.remove(6);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("8:30am")){
                        horasList.remove(7);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("9:00am")){
                        horasList.remove(8);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("9:30am")){
                        horasList.remove(9);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("10:00am")){
                        horasList.remove(10);
                        adapter.notifyDataSetChanged();

                    }
                    if(hora.equals("10:30am")){
                        horasList.remove(11);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("11:00am")){
                        horasList.remove(12);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("11:30am")){
                        horasList.remove(13);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("12:00pm")){
                        horasList.remove(14);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("12:30pm")){
                        horasList.remove(15);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("1:00pm")){
                        horasList.remove(16);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("1:30pm")){
                        horasList.remove(17);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("2:00pm")){
                        horasList.remove(18);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("2:30pm")){
                        horasList.remove(19);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("3:00pm")){
                        horasList.remove(20);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("3:30pm")){
                        horasList.remove(21);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("4:00pm")){
                        horasList.remove(22);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("4:30pm")){
                        horasList.remove(23);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("5:00pm")){
                        horasList.remove(24);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("5:30pm")){
                        horasList.remove(25);
                        adapter.notifyDataSetChanged();


                    }
                    if(hora.equals("6:00pm")){
                        horasList.remove(26);
                        adapter.notifyDataSetChanged();


                    }
                    }


            } else {
                Log.d("score", "Error: " + e.getMessage());
            }
        }
    });
Felipe Franco
  • 117
  • 1
  • 12

3 Answers3

2

Java 8

I think you can use Java 8 streams and lambda expressions to do this like below

Set<String> citasHoras = citasTaller.stream().map(citas -> citas.getString("Hora")).collect(Collectors.toSet());

horasList.removeIf(horas -> citasHoras.contains(horas));

The first line above will extract the Hora from citasTaller list for each item into a Set of String.

Then the second line will just remove the "Horas" from the horasList


You can change your language level in Android Studio following this link

If you don't want to change your language level than in level 7 you can use the following code.

Java 7

for(ParseObject parseObject : citasTaller){
    String hora = parseObject.getString("Hora");
    if(horasList.contains(hora)){
        horasList.remove(hora);
    }
}
muasif80
  • 2,978
  • 2
  • 24
  • 36
  • muasif80 i updated a screenshot... how can i do the java 8 will this after my project? Is there any other way to do this? – Felipe Franco Jun 07 '19 at 03:25
  • Please just change the language level to Java 8. – muasif80 Jun 07 '19 at 03:35
  • 1
    @FelipeFranco I have updated the answer, please check now. – muasif80 Jun 07 '19 at 04:13
  • I m pleased for this answer is helpful. Also kindly consider accepting the answer. – muasif80 Jun 07 '19 at 16:25
  • Hello muasif80 i updated an image so that you can see it tells me something about the SDK but i need this to work in all apis... by the way i want to accept the answer but i dont know where, can you please guide me on this? – Felipe Franco Jun 07 '19 at 19:32
  • Ok please change your minimum sdk level to 24 using this info in this link https://stackoverflow.com/a/27626671/578855 – muasif80 Jun 07 '19 at 19:58
  • @FelipeFranco I have updated the answer. Actually I had made my language level to 7 and tried the previous code and it worked. But now it appeared out that removeIf is also from Java8 which is not available in the minSdkVersion below 24. So its ok you can use this conventional code that I have put above. The newer versions have better smaller code options. – muasif80 Jun 07 '19 at 20:05
  • What i can see while debugging the app is that horasList.contains(hora) is false so it never enters the if function :( i will update my full class so that you see! @muasif80 – Felipe Franco Jun 08 '19 at 23:43
  • https://ideone.com/BMvxNX This is a code I have setup to give you an idea. The contains return success on exact match. So please check your data. I m not sure what data you have. If it's having spaces around it trim it. If it has leading 0 then concatenta leading 0 after checking length of string less than 7 for the value and then do this contains method. Such that both lists have times in same format. These could be a few issues. But yes I m not having full picture of your data. The code snippet I provided seems to be working fine. I tested that. – muasif80 Jun 09 '19 at 00:37
  • Hello @muasif80 ive seen what you did in previous comment but the String from both list have the same format. ive updated an idea i have in the code.once the hour is equal to the string the index is removed and the list is also removed in 1 so the index changes for every value.The only way that remove has worked for me is placing the position or index! – Felipe Franco Jun 09 '19 at 01:30
  • Are you using index of elements in horasList in your program somewhere? – muasif80 Jun 09 '19 at 04:07
  • Indices update in horasList has no effect on our loop and the contains method starts each time from first item in horasList and search 1 by 1. – muasif80 Jun 09 '19 at 04:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194656/discussion-between-felipe-franco-and-muasif80). – Felipe Franco Jun 09 '19 at 05:58
1

You need to use Set (HashSet) for your task. It was made specifically for tasks like that, when you need to check:

You can also create Set from the List:

Set<Foo> foo = new HashSet<Foo>(myList);

Andrei Konstantinov
  • 6,073
  • 4
  • 34
  • 55
1

I found the solution to this problem is very simple..

THIS GIVES YOU THE POSITION IN WITCH HORA IS IN THE LIST. THEN YOU GET THE LIST AND REMOVE THE GIVEN POSITION!.

int posi=  horasList.indexOf(hora);
 horasList.remove(posi);
Felipe Franco
  • 117
  • 1
  • 12