Android/Java: Import text file and read out random lines from within the text file
I got this code I've been working on so far, it is going to be a game im trying to create which just switches between two players. Every time it switches it is supposed to write out a question, like truth or dare. It is not allowed to write the same question twice, and should therefore be able to see if it has already used that question.
So far I've got it running, and it switches between the two players every time you hit Next.
But what I have a lot of trouble with, is fetching the data from within the txt file called man, here there is three lines, text1 text2 and text3. It should be able to take these randomly and know if it has already read one. I can not get the current InputStream to work, it says the man file is int, but it contains string?
Here is the code so far:
package truthordare;
import java.io.FileInputStream;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
//import android.content.Intent;
public class truthordareActivity extends Activity
{
public int i = 0;
//public String man;
//public String woman;
TextView w;
TextView m;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
{
final Button buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
gameCode();
}
});
}
}
/*public FileInputStream openFileInput(int man) throws IOException
{
String collected = null;
try
{
FileInputStream fis = openFileInput(R.raw.man);
byte[] dataArray = new byte[fis.available()];
while (fis.read(dataArray) != -1)
{
collected = new String(dataArray);
}
fis.close();
}
catch (IO开发者_如何学GoException e)
{
e.printStackTrace();
}
return null;
}
*/
public void gameCode()
{
// Perform action on click
setContentView(R.layout.game);
if(i == 0)
{
w = (TextView)findViewById(R.id.textView1);
w.setText("This is a player1");
i = 1;
final Button buttonNext = (Button) findViewById(R.id.buttonNext);
buttonNext.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
gameCode();
}
});
}
else
{
m = (TextView)findViewById(R.id.textView1);
m.setText("This is player2");
i = 0;
final Button buttonNext = (Button) findViewById(R.id.buttonNext);
buttonNext.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
gameCode();
}
});
}
}
}
Have you considered using an XML-file for your questions? From the information you provided, the structure should be like this:
<questions>
<question>
<id>1</id>
<text>This is question nr. 1</text>
</question>
<question>
<id>2</id>
<text>This is question nr. 2</text>
</question>
<question>
<id>3</id>
<text>This is question nr. 3</text>
</question>
</questions>
Load all the questions into a List/ArrayList as Question-objects and when a question is asked - remove it from the list. If you need to save the questions for later, don't remove them but rather save the ID's of all asked questions in another list, and when you try to get the next question make sure its ID is not in the list of ID's.
To get a random question you just use a random number generator that provides you with values between 0 and list.size().
This way you won't have to spend time opening and closing InputStreams all the time and you have an easy way of making sure that a question is only asked once.
Grab an InputStream
using Resources.openRawResource
. In your case this is getResources().openRawResource(R.raw.man)
.
精彩评论