java - 如何从 java 类中读取内容?

标签 java android

我刚刚开始接触 Android 开发和 Objective 编程,所以这个问题对你来说太愚蠢了..我知道,但我想问,我如何从 java 类中读取内容?

这是我的情况:我来自 C 和 C++,我必须保留 XML 文件中的数据。这些数据是关于地点的,因此项目是:“姓名”、“地址”、“电话号码”等。现在,如果我使用 C,也许我会做一个数组或列表,但是,我想学习 Java 和目标编程,所以我创建了一个名为“PointOfInterest”的类,其中包含所有字段,但我不知道我是否写得很好,但我不知道如何从类里面阅读。如果我在 C 或 C++ 中使用 to for cicles,例如,当标题等于字段“0”时,我可以水平移动。但是对于 Java 类??

我该怎么办?这是我的代码:

package com.example.findmyclients;

public class PointOfInterest {
    private String title;
    private String address;
    private String telephone;
    private String email;
    private String description;
    private String facebook;
    private String twitter;
    private String website;

    public void AnyPoint(String nome, String indirizzo, String telefono, String email, String description, String facebook, String twitter, String website){
        //super();
        this.title = nome;
        this.address = indirizzo;
        this.telephone = telefono;
        this.email = email;
        this.description = description;
        this.facebook = facebook;
        this.twitter = twitter;
        this.website = website;
    }

    public String prova(String nome){
        if(title.contains(nome)){
            return this.address;
        }
        return nome;
    }
}

这是我“尝试”保留 xml 中的类数据:

for (int i = 0; i < markers.getLength(); i++) {

    //PointOfInterest p = point.get(i+1);

    Element item = (Element) markers.item(i);
    String name = item.getAttribute("name");
    String address = item.getAttribute("address");
    String stringLat = item.getAttribute("lat");
    String stringLong = item.getAttribute("long");
    String icon = item.getAttribute("icon"); //assigned variable for the XML icon attribute

    String desc = item.getAttribute("descrizione");
    String tel = item.getAttribute("tel");
    String email = item.getAttribute("email");
    String facebook = item.getAttribute("facebook");
    String twitter = item.getAttribute("twitter");
    String web = item.getAttribute("web");

    Double lat = Double.valueOf(stringLat);
    Double lon = Double.valueOf(stringLong);

    PointOfInterest p = new PointOfInterest();
    p.AnyPoint(name,address,tel,email,desc,facebook,twitter,web);

[...]

最佳答案

我担心会误解你想要得到的东西,确切地说我没有理解“如何从类里面阅读”的含义

尽管如此,如果我理解正确的话,您可能想要使用 Java Collections Framework ,例如 HashMap 或 TreeMap。

如果你想获取带有某个“key”的 PointOfInterest 实例,你可以按如下方式操作。

Map<String, PointOfInterest> nameMap = new HashMap<<String, PointOfInterest>();
nameMap.put(p.getName, p);


PointOfInterest anotherP = nameMap.get("foo");

if (anotherP != null)  {  // there is a instance with a name of "foo"
   ...
} 

顺便说一句,我强烈建议使用 Immutable 对象。 例如

public class PointOfInterest {
    private final String title;
    private final String address;
    private final String telephone;
    ....

    //constructure
    public PointOfInterest(String title, String address, ... ) {
        this.title = title;
        ....
    }
  ....
 }

如果这不是你想知道的,请让我理解。 我希望有一种方法可以帮助你。

关于java - 如何从 java 类中读取内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26336288/

相关文章:

android 布局重叠 ListView 和 LinearLayout

java - 有没有办法从处理程序中删除匿名 Runnable?

android - 缩小两个 Activity 之间的过渡动​​画

java - 为什么 sleep 方法会影响/不在面板上显示我更新的 JTextArea?

java - UNIX:java 程序命令从终端运行但不能从 bash 脚本运行

java - 在 Linux 上自动转码 H.264 视频

Android 硬件和 Adob​​e AIR 续

java - 使用 Arquillian 时如何引用放置在 WEB-INF 文件夹中的文件?

java - JavaFX 应用程序的 ANT JAR 文件

java - 字符串资源 XML 文件是否允许无效的 Java 变量作为名称属性?