简体   繁体   中英

Find words in text file from assets folder ANDROID STUDIO

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


        }
    });
}

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM