与列表进行比较并从 Spinner 中删除项目的逻辑 android

Logic comparing to list and removing item from Spinner android

目前我有两个列表,一个是我自己做的,另一个是来自 Parse。

第一个列表...

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;
}
   }

另一个列表有 8:00am 和 9:30am 我的第一个问题是我可以比较不同大小的列表吗???。

这就是我此刻的想法..

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++;
        }

    }

还没想好在 if 语句中放什么..

我的目标是,如果字符串在 horasList 中,并且在 citasTaller 中,则要从 Spinner 中删除。我完成了将随机位置放入其中...

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

所以它应该像

horasList.remove(horaCita)

一定有更好的方法来做到这一点。按照这个顺序,我需要从 horasList 中删除 8:00am 和 9:30am.

顺便说一句我做的..

horasList = UtilAppFuntions.getHoraDia();

这是答案对应代码的更新...

所以我这样做了,但是我有一个问题@muasif80,一旦小时等于字符串,索引被删除并且列表也在 1 中被删除,所以每个值的索引都会改变..

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());
            }
        }
    });

您需要使用 Set (HashSet) 来完成您的任务。 它是专门为这样的任务而制作的,当你需要检查时:

您还可以从列表中创建集合:

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

Java 8

我认为您可以使用 Java 8 个流和 lambda 表达式来执行此操作,如下所示

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

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

上面的第一行将从 citasTaller 列表中为每个项目提取 Hora 到一组字符串中。

然后第二行将从 horasList

中删除 "Horas"

您可以在 Android Studio 中更改您的语言级别,然后

如果您不想更改您的语言级别而不是级别 7,您可以使用以下代码。

Java 7

for(ParseObject parseObject : citasTaller){
    String hora = parseObject.getString("Hora");
    if(horasList.contains(hora)){
        horasList.remove(hora);
    }
}

我发现这个问题的解决方案非常简单..

这为您提供了 列表中的位置。 然后你得到列表并删除给定的位置!.

int posi=  horasList.indexOf(hora);
 horasList.remove(posi);