我试图在textview中显示arraylist中提取的字符串。但它没有显示字符串。但是当我设置text ="有些"在我的xml文件中。然后它显示了价值。
下面我附上了我的MainActivity.java和activity_main.xml文件。
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>
答案 0 :(得分:0)
您在addWord()
try {
addWord();
} catch (IOException e) {
e.printStackTrace();
}
您只需在文件上创建FileReader
words.txt
BufferedReader in = new BufferedReader(new FileReader("words.txt"));
考虑你的考试
InputStream inputStream = assetManager.open("words.txt");
第一个找不到任何文件。您也应该在addWord()
中正确加载资源。
InputStreamReader fin = new InputStreamReader(assetManager.open("words.txt"));
BufferedReader in = new BufferedReader(fin);