0

我有一个包含此信息的文本文件

堪培拉赌场;21 Binara Street, Canberra ACT, 2601;Canberra Casino 是一家赌场,位于澳大利亚首都堪培拉中部的 Civic。与澳大利亚其他赌场相比,赌场相对较小。;(02) 6257 7074;www.canberracasino.com.au

堪培拉国家博物馆;Parkes Place, Canberra ACT, 2601;澳大利亚国家博物馆探索澳大利亚的土地、国家和人民。除圣诞节外,每天上午 9 点至下午 5 点开放。一般入场免费。;(02) 6240 6411;www.nga.gov.au

存储在 sdcard 中

在此之后,我使用此方法检索值

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);


    }}
4

1 回答 1

0

您可能应该考虑使用 Application 类。您可以将 Application 视为一种无 GUI 活动,其工作方式类似于遵循 MVC 模式的程序中的模型。您可以将所有读取对象放入应用程序中的数据结构中,然后使用您自己设计的访问器和修改器访问它们。

看看这个官方文档

就您的代码而言,您只能通过获取对 FileReader 类的引用来访问您的 read 实例,但是您的两个活动是独立的实体。你必须做这样的事情:

// 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,然后在您有更多经验时返回您的项目。

于 2013-05-08T04:01:22.757 回答