Comparing textView with txt using Android arrayList

1

I'm putting together a very simplistic project in which I display the processes that are running on android and compare them to a list in a txt file that has other processes. The intent is to verify that all processes that are appearing in the textView are in the txt. If something appears in the textView and this is not in TXT, send an alert showing what is in the textView and not in txt. I thought of a solution using arraylist

1 - in the textview, the output is like this

com.whatsapp
com.snaptube.premium
com.youtube
com.facebook

separated by a line break

My text file is the same, but with more processes. Example

com.whatsapp
com.snaptube.premium
com.youtube
com.facebook
com.outroApp

The purpose of the textview is to use texview some application that is not in txt and the user will receive a warning that the application is not in txt. it would be as if each line of the textview were compared to all lines of the text file. For that I thought of 2 for's

What I have now:

ArrayList<String> texto = new ArrayList<String>(); // array para salvar o que esta no arquivo texto
ArrayList<String> textView= new ArrayList<String>(); // array para salvar o que mostra no textview




            while ( (recebe_string = bufferedReader.readLine()) != null ) {
                texto.add(recebe_string); // adiciono o que tenho no txt em um array

            while ((line = reader.readLine()) != null) { // exibindo no textview                                                         
                textView.add(parteFinal); //adiciono os processos que estao no textview em um array

Now comes the part that I can not. Make comparison of what it has in textview with txt

I thought about it

for (int i = 0; i < texto.size() ;i++) { // percorro array do txt
            for (int ii = 0; ii <textView.size() ; ii++) { // percorro array do textview
                if (textView.get(ii).equals(texto.get(i))) // se tudo que tem no textview tem no txt   nada a fazer
                    teste = false;
                else{ // se tem algo no textView que nao tem no txt, avisa
                    teste = true;
                    virus = textView.get(ii); // capturo o que tem no textview e nao tem no txt
                    System.out.println("virus"+ textView.get(ii)); // exibo o que capturei                       
                }
            }
            }

I'm having a hard time comparing. this method I did it even warns that the process displayed in the textView is in txt

    
asked by anonymous 17.10.2015 / 16:36

1 answer

0

Your logic is wrong in several ways:

  • Does not leave the inner loop when it encounters a match.
  • As the warning is inside the loop , if the application you are looking for is not the first one, even if it is in the text, the warning will be displayed.
  • There may be more than one application that is not in the text.

I propose that the process be done in 2 phases:

  • First get a list of applications that are not in the textView.
  • According to submitting a warning for each of them.

Code

//Encontrar os virus
ArrayList<String> virusList = new ArrayList<String>(); 
for (int i = 0; i < textView.size() ;i++) { // percorro o array textView
    boolean NaoExiste = true;
    for (int ii = 0; ii < texto.size(); ii++) { // percorro o array texto
        if (textView.get(i).equals(texto.get(ii))){ // Foi encontrada uma correspondência
            NaoExiste = false;
            break; //termina o loop interior
        }
    }
    if(NaoExiste){
        virusList.add(textView.get(i)); //Adiciona à lista de virus
    }
}

//Apresentar os avisos
for(String virus : virusList){
    System.out.println("virus "+ virus);
}

The Wakim comment led me to find a simpler way to do what you want, using the removeAll () of ArrayList:

//Faz uma cópia do array textView
ArrayList<String> virusList = new ArrayList<>(textView);

//Remove os itens de textView que existem em texto
virusList.removeAll(texto);

//Agora virusList contém os itens de textView que não existem em texto.

//Apresentar os avisos
for(String virus : virusList){
    System.out.println("virus "+ virus);
}
    
17.10.2015 / 17:26