简体   繁体   中英

SetText is not showing text in TextView

I am trying to show the fetched string from arraylist in textview. but it is not showing the string. but when i set text="Some" in my xml file. then it shows the value.

Below i have attached my MainActivity.java and activity_main.xml file.

MainActivity.java:

package com.example.dhara.codesprint;

import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;


public class MainActivity extends AppCompatActivity {


    //private WordDictionary dictionary;
    private static final int DEFAULT_WORD_LENGTH = 3;
    private static final int MAX_WORD_LENGTH = 7;
    private ArrayList<String> wordList = new ArrayList<>();
    private Random random =new Random();
    private String currentWord;
    private ArrayList<String> words;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText text7 = (EditText) findViewById(R.id.edittext);
        Button checkAnswer = (Button) findViewById(R.id.button);
        Button reset = (Button) findViewById(R.id.button2);
        AssetManager assetManager = getAssets();
        try {
            InputStream inputStream = assetManager.open("words.txt");
        } catch (IOException e) {
            Toast toast = Toast.makeText(this, "Could not load dictionary", Toast.LENGTH_LONG);
            toast.show();
        }

        try {
            addWord();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addWord() throws IOException {

        BufferedReader in = new BufferedReader(new FileReader("words.txt"));
        String line;
        while ((line = in.readLine()) != null) {
            String word = line.trim();
            if (word.length() == 6) {
                wordList.add(word);
            }
            int x,y,temp;
            String str;
            x=random.nextInt(wordList.size());
            str=wordList.get(x);
            TextView textView1 = (TextView) findViewById(R.id.textView1);
            textView1.setText("Text");
        }
    }

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/background"
    tools:context=".MainActivity">

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignRight="@+id/imageView1"
        android:layout_alignEnd="@+id/imageView1"
        android:id="@+id/textView1"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_alignStart="@+id/imageView1"
        android:textAlignment="center"
        android:textStyle="normal|bold"
        android:textColor="@android:color/background_light" />

</RelativeLayout>

You're suppressing a possible error in addWord()

try {
    addWord();
} catch (IOException e) {
    e.printStackTrace();
}

where you just create a FileReader on a file words.txt

BufferedReader in = new BufferedReader(new FileReader("words.txt"));

Considering your test

InputStream inputStream = assetManager.open("words.txt");

the first won't find any file. You should load the asset correctly in addWord() too.

InputStreamReader fin = new InputStreamReader(assetManager.open("words.txt"));
BufferedReader in = new BufferedReader(fin);

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