Im trying to get my android application to read a .txt file and display a random line in the text view,
A snippet of the code i got so far is
{
try
{
java.util.ArrayList<String> lines = new java.util.ArrayList<String>();
FileReader fileReader;
BufferedReader reader;
fileReader = new FileReader("MyStrings.txt");
reader = new BufferedReader(fileReader);
String lineIn = reader.readLine(); //This String will hold the data brought in fron the text file.
do
{
System.out.println(lineIn);
lineIn = reader.readLine();
}while(lineIn != null);
reader.close();//This step is not necessary but is advised
ArrayList<String> x = new A开发者_C百科rrayList<String>();
Random rand = new Random();
//This line gets your question
//TextView t1v = (TextView) findViewById(R.id.factslist);
//TextView tv = (x.get(rand.nextInt(x.size()-1)));
TextView t = (TextView) findViewById(R.id.factslist);
I found this through a tutorial, but i cant get it to show a random line. Any ideas whats up? ( there is some random stuff in there that isnt used)
XML File
<TextView
android:gravity="center_vertical|center_horizontal"
android:id="@+id/factslist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textStyle="bold"
android.layout_column="15"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
There are two steps involve
//Give your range for random line numbers.
int min = 1;
int max = 10;
Random r = new Random();
int someRandomNo = r.nextInt(max - min + 1) + min;
String strTextToDisplay = getStreamTextByLine("MyStrings.txt", someRandomNo);
TextView t = (TextView) findViewById(R.id.factslist);
t.setText(strTextToDisplay);
Below is the usefull function to get string of particular line number of text file placed in assets folder.
private String getStreamTextByLine(String fileName, int lineNumber) {
String strOut = "";
String line = "";
int counter = 1;
AssetManager assetManager = getAssets();
try {
InputStream in = assetManager.open(fileName);
if (in != null) {
InputStreamReader input = new InputStreamReader(in);
BufferedReader buffreader = new BufferedReader(input);
while ((line = buffreader.readLine()) != null) {
if (counter == lineNumber) {
strOut = line;
}
counter++;
}
in.close();
} else {
Log.e("Input Stream Problem",
"Input stream of text file is null");
}
} catch (Exception e) {
Log.e("0003:Error in get stream", e.getMessage());
}
return strOut;
}
From my experience you kind of need to use an Input Stream, Stream Reader, and Buffered reader to read from a file in android. It's not as simple as running java from your command line. This is a code excerpt from an app of mine that I heavily modified for your usage. good luck.
This code assumes the random number is within the scope of the file. If not, it will return null
. The best way to assure that RandNum
is within the scope of the file is to find the number of lines in the file (google that if you don't know how to do it) and perform a modulo operation such as:
int RandNum = //gererate random number code
RandNum = RandNum % linecount + 1;
RandNum
will then be a number between 1 and the number of lines in the file.
I then pass this to our function:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public String[] GetRandLine (File file, int RandNum){
try{
FileInputStream fis = openFileInput(String.valueOf(file));
InputStreamReader isr = new InputStreamReader (fis, "UTF8");
BufferedReader in = new BufferedReader (isr);
int i = 1;
String YourResult;
while ((YourResult=in.readLine()) != null) { //makes sure line is not null
if (i == RandNum) return YourResult;
i++;
}
in.close();
isr.close();
fis.close();
return null; //should never get here hypothetically
}
catch (IOException e){return null;} //Stream could not be opened
}
精彩评论