Comparing textView to txt using Android arrayList

Asked

Viewed 128 times

1

I’m putting together a very simplistic project in which I show the processes that are running on android and compare it to a list in a txt file that has other processes. The intention is to check if all the processes that are appearing in textView, are in txt. If something appears in textView and this is not in TXT, be sent an alert showing what is in textView and not in txt. I thought of a solution using arraylist

1- in 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, only with more processes. Example

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

the intention is, if texview appears some application that is not in txt the user receives a warning showing that that application is not in txt. it would be as if each line of the textview were compared with all lines of the text file. That’s why 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 I can’t. Compare what you have in textview with txt

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 trouble comparing this part. this method I did it warns even if the process displayed in textView is in txt

1 answer

0


Your logic is wrong in many ways:

  • Does not leave the loop inside when you find a match.
  • As the warning lies within the loop interior, if the application to search is not the first, even if it is found 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 two stages:

  • First get a list of apps that are not in textView.
  • According to present a notice 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);
}

Wakim’s comment led me to find a simpler way to do what you want, using the method removeAll() 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);
}
  • Just a suggestion, wouldn’t it be simpler to use a Set to avoid iterating always (would only have a hehe)?

  • No problem :D

  • @Wakim You’re right, it didn’t occur to me to use a Set. I usually start from the AP code to build my answer. I think your suggestion would be: HashSet<String> virus = new HashSet<>(texto); virus.removeAll(textView);

  • Thanks for the answers. However it is displaying the total content of TXT

  • Then it is because the strings are not equal in the two Arraylist or have spaces or special characters. The code I posted is correct (one was missing { see the edit). Try to do trim() in strings when comparing: textView.get(ii).trim().equals(texto.get(i).trim())

  • if the problem is special characters, I could use the contains()?

  • Not because you do not know who contains what. Anyway it would not be the best solution. The solution is to get the strings correctly.

  • When analyzing the output, I saw that it shows everything you see in TXT except what is in the text view. what I wanted is the opposite of would be because of that line? virusList.add(text.get(i));

  • I’ve been reading the comments in your code and in fact what you want is the opposite. I’ll edit the answer.

  • I expressed myself badly at that stage. then would just replace the for counters and in the add part in the arraylist virus, pick up what’s in the text view? I tried it and it didn’t work

  • apparently it is working now thank you very much

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.