0

I have a problem with my app. What I'm trying to do is to find rhymes to a word entered from the user into a text field. I have a dictionary in assets folder called "words.txt". My app is working almost correctly, but it finds only one word from my text file. How can I find all words that rhyme with the word from the text field? Any ideas? Here is my code:

@Override
protected void onCreate(Bundle saveInstanceState){
    super.onCreate(saveInstanceState);
    setContentView(R.layout.activity_main);

    editTextWord = (EditText)findViewById(R.id.editTextWord);
    b_read = (Button)findViewById(R.id.buttonSearch);
    tv_text = (TextView)findViewById(R.id.nameTxt);
    b_clear = (Button)findViewById(R.id.buttonClear);



    b_clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, MainActivity.class);
            startActivity(i);
        }
    });

    b_read.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {
                InputStream is = getAssets().open("words.txt");
                int size = is.available();
                BufferedReader rd = new BufferedReader(new InputStreamReader(is,"UTF-8"));
                String line;
                String word = editTextWord.getText().toString();
                if (word.isEmpty()){
                    Toast.makeText(getApplicationContext(), "Wpisz słowo", Toast.LENGTH_SHORT).show();
                    return;
                }

                while ((line = rd.readLine()) !=null ){
                    if (line.substring(line.length()-2).equals(word.substring(word.length()-2))){
                        tv_text.setText(line);

                    }
                }
                rd.close();

            }catch (IOException ex){
                ex.printStackTrace();
            }


        }
    });
}
amacf
  • 257
  • 2
  • 8
MichalG
  • 13
  • 3

1 Answers1

0

tv_text.setText overwrites anything that is already in the TextView. You need to use something like TextView.append(CharSequence)

Try:

tv_text.append(line + "\n");
amacf
  • 257
  • 2
  • 8
  • great, it works, but now how i can display it one under next? now it looks rly bad – MichalG Sep 19 '17 at 16:00
  • You need to add new lines. Maybe try: tv_text.append(line + "\n"); – amacf Sep 19 '17 at 16:05
  • I have one more question ;) Some words have alot of results after clicking button, and they are not displaying. What i should do in this situation? i was thniking about somethnik to rewind the list of results. Any suggestions what i should read/learn about? – MichalG Sep 19 '17 at 16:39
  • Can you clarify what you mean? Do you mean that some results overflow the TextView? Or that the words from a previous word are not cleared from the list? Or that you are missing words from your list that you expect to see? – amacf Sep 19 '17 at 16:45
  • I mean for some words, there is too much results to show it in TextViev, so it disply only like 30 results, where there are much more, so yes, some results overflow TextViev. I should use some list or smth like that? – MichalG Sep 19 '17 at 16:54
  • I would use a recyclerview or a listview. Maybe you could even try to insert your textview into a scrollview. – Fer Sep 19 '17 at 17:32
  • Yes, I would just try wrapping your TextView in a ScrollView. See the second response [here](https://stackoverflow.com/questions/23873454/android-textview-scrollable) – amacf Sep 19 '17 at 17:48
  • Great, it works :) i have one, and i guess last question. I put something else to my code. "while ((line = rd.readLine()) !=null ){ if (line.substring(line.length()-2).equals(word.substring(word.length()-2))){ tv_text.append(line + "\n"); }else{ Toast.makeText(getApplicationContext(), "Brak rymu dla słowa " + word, Toast.LENGTH_SHORT).show(); and it works, if there is no rhymes for some word, it shows toast, but the Toast is showing even after i click on the button "czyść" which restart activity. what is wrong in my code? – MichalG Sep 19 '17 at 18:45
  • That will create one toast for every word in the list that does not rhyme. Instead, create a boolean, default it to false. If you find a rhyming word, set it to true. After the while loop, if it is still false, then produce a toast that no rhymes were found. – amacf Sep 20 '17 at 13:43