所以我正在为一个最终项目的Android课程做一个创建你自己的冒险游戏/书。我设置它的方式是两个活动(菜单和页面布局)。我有一个"主文件"具有每个页面之间关系的文本文件(下面的示例)。我也有"页面"在需要检索的文本文件中设置,然后设置为TextView和3按钮。 (以下示例说明如何设置。
我的问题是我应该如何正确地检索这些信息。
我想让它设置当Page类执行OnCreate时,它会填充textview和正确的Page.txt文件中的3个按钮。
这是我的page.class页面的代码。
public class pages extends AppCompatActivity {
ArrayList<FileRelationship> fileRelationships;
FileRelationship selectedRelationship;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
fileRelationships = new ArrayList<FileRelationship>();
String MasterLine;
InputStream inputStream = this.getResources().openRawResource(R.raw.MasterFile);
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader buffreader = new BufferedReader(inputreader);
// Creates the arrayList of all the relationships for later use of retreival.
try {
while (( MasterLine = buffreader.readLine()) != null) {
String[] MasterLineArray = MasterLine.split(",");
String filename = MasterLineArray[0];
String choice1 = MasterLineArray[1];
String choice2 = MasterLineArray[2];
String choice3 = MasterLineArray[3];
FileRelationship fr = new FileRelationship(filename, choice1, choice2, choice3);
fileRelationships.add(fr);
}
} catch (IOException e) {
}
TextView storyText = (TextView) findViewById(R.id.storyText);
Button choice1 = (Button) findViewById(R.id.firstchoice);
choice1.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
findRelationship(selectedRelationship.firstLink);
finish();
startActivity(getIntent());
}
});
Button choice2 = (Button) findViewById(R.id.secondchoice);
choice2.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
findRelationship(selectedRelationship.secondLink);
finish();
startActivity(getIntent());
}
});
Button choice3 = (Button) findViewById(R.id.thirdchoice);
choice3.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
findRelationship(selectedRelationship.thirdLink);
finish();
startActivity(getIntent());
}
});
// **THIS IS THAT PART I NEED HELP WITH THE MOST** I dont know what
// to put in the setText part to have it retrieve the correct information.
storyText.setText();
choice1.setText();
choice2.setText();
choice3.setText();
}
public void findRelationship(String nextFileName) {
int pageIndex = fileRelationships.indexOf((new FileRelationship(nextFileName)));
selectedRelationship = fileRelationships.get(pageIndex);
}
}
这是我的FileRelationship类,我用它在我的页面类中创建数组。
public class FileRelationship {
String fileName;
String firstLink;
String secondLink;
String thirdLink;
public FileRelationship(String fileName) {
this.fileName = fileName;
}
public FileRelationship(String fileName, String firstLink, String secondLink, String thirdLink) {
this.fileName = fileName;
this.firstLink = firstLink;
this.secondLink = secondLink;
this.thirdLink = thirdLink;
}
public boolean equals(Object o) {
if(o == null) {
return false;
}
if(this == o) {
return true;
}
if(o instanceof FileRelationship) {
return this.fileName.equals(((FileRelationship)o).fileName);
}
return false;
}
}
这就是我的MasterFile.txt如何设置来定义关系(只是一个例子)
Page1,Page2,Page3,Page4
Page2,Page5,Page6,Page7
Page3,Page8,Page9,Page10
最后这就是我的Page1,Page2,Page3等的设置方式(再次举个例子)
This is a practice story line, my pages text will go into here and create the story in this way.
Choice 1
Choice 2
Choice 3
创建一个从selectedRelationship中检索信息然后将其中所有4个放入字符串的方法的最佳方法是什么?如果是这样,怎么办呢。我也不确定我的FindRelationship方法是否正确。
任何帮助将不胜感激。我觉得我还有其他一切正确,但我也不确定我是否正确检索MasterFile部件并正确地将它放入阵列列表中。任何帮助将非常感激。谢谢!