我有一个包含此信息的文本文件
Casino Canberra; 21 Binara Street,Canberra ACT,2601;堪培拉赌场是位于澳大利亚首都堪培拉中心城市Civic的赌场。与澳大利亚的其他赌场相比,赌场相对较小。;(02)6257 7074; www.canberracasino.com.au
堪培拉国家博物馆; Parkes Place,Canberra ACT,2601;澳大利亚国家博物馆探索澳大利亚的土地,国家和人民。每天上午9点至下午5点开放,圣诞节除外。一般入场免费。;(02)6240 6411; www.nga.gov.au
存储在SD卡
中之后我使用此方法检索值
package au.edu.canberra.g30813706;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Environment;
public class FileReader extends Activity{{
ArrayList<read> sInfo = new ArrayList<read>();
ArrayList<String> sLines = new ArrayList<String>();
String line;
String[] saLineElements;
String txtName = "AccomodationTxt.txt";
File root = Environment.getExternalStorageDirectory();
File path = new File(root, "CanberraTourism/" + txtName);
try {
BufferedReader br = new BufferedReader (
new InputStreamReader(
new FileInputStream(path)));
while ((line = br.readLine()) != null)
{
sLines.add(line);
//The information is split into segments and stored into the array
saLineElements = line.split(";");
//for (int i = 0; i < saLineElements.length; i++)
// sInfo.add(new read(saLineElements[i]));
sInfo.add(new read(saLineElements[0], saLineElements[1], saLineElements[3], saLineElements[4], saLineElements[5]));
}
br.close();
}
catch (FileNotFoundException e) {
System.err.println("FileNotFoundException: " + e.getMessage());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}
但我也有对象类来存储每个单独的项目
package au.edu.canberra.g30813706;
public class read {
public String name;
public String address;
public String info;
public String phone;
public String www;
public read (String name, String address, String info, String phone, String www)
{
this.name = name;
this.address = address;
this.info = info;
this.phone = phone;
this.www = www;
}
}
我唯一的问题是试图在文本视图中显示信息,我不知道如何调用我需要的值
这是我试图插入它的地方
package au.edu.canberra.g30813706;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import au.edu.canberra.g30813706.FileReader;
import au.edu.canberra.g30813706.read;
public class Accommodation_info extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accommodation_layout);
}}
答案 0 :(得分:0)
您应该考虑使用Application类。您可以将Application视为无GUI的活动,其功能类似于遵循MVC模式的程序中的模型。您可以将所有读取的对象放入应用程序中的数据结构中,然后使用您自己设计的访问器和更改器访问它们。
看看这位官方doc。
正如您的代码所代表的那样,您只能通过获取对FileReader类的引用来访问您的读取实例,但您的两个活动是单独的实体。你必须做这样的事情:
// This is the main activity and should be launched first
// Check your manifest to make sure it launches with this activity
package au.edu.canberra.g30813706;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import au.edu.canberra.g30813706.FileReader;
import au.edu.canberra.g30813706.read;
public class Accommodation_info extends Activity
{
// Declare the file reader so you'll have a reference
FileReader reader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accommodation_layout);
// Instantiate the file reader
reader = new FileReader();
// Now you can access the array inside FileReader
// obviously, you need to have a text view called my_textView defined in the
// layout file associated with this activity
TextView myTextView = (TextView)findViewById(R.id.my_textView);
// displays the first element in FileReader's array list
myTextView.setText((String)reader.get(0));
}}
目前,您可能对目前对Android和/或Java的理解有所了解。我建议您尽可能多地使用代码示例,熟悉Android,然后在获得更多经验时再回到项目中。