Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将一个对象添加到另一个java对象的数组中_Java_Android_Object_Xml Parsing - Fatal编程技术网

如何将一个对象添加到另一个java对象的数组中

如何将一个对象添加到另一个java对象的数组中,java,android,object,xml-parsing,Java,Android,Object,Xml Parsing,我正在开发一个Android应用程序。我的应用程序有一个按钮,当我按下此按钮时,我将解析一个XML文件,将此文件的信息放入某个对象中,并将此对象显示在可扩展列表中。 此外,我的XML文件具有以下结构: <?xml version="1.0" encoding="UTF-8"?> <Programs> <Program programNumber="1" imgURL="http://www.photovideolife.com/userfiles/Place

我正在开发一个Android应用程序。我的应用程序有一个按钮,当我按下此按钮时,我将解析一个XML文件,将此文件的信息放入某个对象中,并将此对象显示在可扩展列表中。 此外,我的XML文件具有以下结构:

<?xml version="1.0" encoding="UTF-8"?>
<Programs>
    <Program programNumber="1" imgURL="http://www.photovideolife.com/userfiles/Placeholder%2001.jpg" description="Lorem ipsum dolor sit er elit">
        <Episode pN="1" episodeNumber="1" transmissionName="Titolo" date="29 Giu 2013" time1="14:30" time2="" channel="Real Time" channelLogo="https://lh6.googleusercontent.com/-XSh-9ngYJu4/ThiY-xl2EeI/AAAAAAAABmc/iz04VpL5hOs/s800/realtime.png">
        </Episode>
        <Episode pN="1" episodeNumber="1" transmissionName="Titolo" date="29 Giu 2013" time1="" time2="16:30" channel="DMAX" channelLogo="http://tv.zam.it/canali/dmax.png">
        </Episode>
        <Episode pN="1" episodeNumber="2" transmissionName="Titolo" date="01 Lug 2013" time1="14:30" time2="" channel="Real Time" channelLogo="https://lh6.googleusercontent.com/-XSh-9ngYJu4/ThiY-xl2EeI/AAAAAAAABmc/iz04VpL5hOs/s800/realtime.png">
        </Episode>
        <Episode pN="1" episodeNumber="2" transmissionName="Titolo" date="01 Lug 2013" time1="" time2="16:30" channel="DMAX" channelLogo="http://tv.zam.it/canali/dmax.png">
        </Episode>
    </Program>
</Programs>
Program.java

public class Episode {
    String pN, episodeNumber, transmissionName, date, time1, time2, channel, channelLogo;

    public String getpN() {
        return pN;
    }

    public void setpN(String pN) {
        this.pN = pN;
    }

    public String getEpisodeNumber() {
        return episodeNumber;
    }

    public void setEpisodeNumber(String episodeNumber) {
        this.episodeNumber = episodeNumber;
    }

    public String getTransmissionName() {
        return transmissionName;
    }

    public void setTransmissionName(String transmissionName) {
        this.transmissionName = transmissionName;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime1() {
        return time1;
    }

    public void setTime1(String time1) {
        this.time1 = time1;
    }

    public String getTime2() {
        return time2;
    }

    public void setTime2(String time2) {
        this.time2 = time2;
    }

    public String getChannel() {
        return channel;
    }

    public void setChannel(String channel) {
        this.channel = channel;
    }

    public String getChannelLogo() {
        return channelLogo;
    }

    public void setChannelLogo(String channelLogo) {
        this.channelLogo = channelLogo;
    }

}
public class Program {
    public Episode[] episodes;

    String programNumber, imgUrl, description;

    public Episode[] getEpisodes() {
        return episodes;
    }

    public void setEpisodes(Episode[] episodes) {
        this.episodes = episodes;
    }

    public String getProgramNumber() {
        return programNumber;
    }

    public void setProgramNumber(String programNumber) {
        this.programNumber = programNumber;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}
public class Programs {
    public Program[] programs;

    public Program[] getPrograms() {
        return programs;
    }

    public void setPrograms(Program[] programs) {
        this.programs = programs;
    }
}
import it.lucgian84.models.Episode;
import it.lucgian84.models.Program;
import it.lucgian84.models.Programs;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.renderscript.Element;
import android.util.Log;

public class XmlParser {
    private String xml;
    private Programs programs;
    private Program program = new Program();
    private Episode episode = new Episode();

    public XmlParser(String xml) {
        this.xml = xml;
    }

    public void parseXml() {
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory
                    .newDocumentBuilder();
            Document document = documentBuilder.parse(new InputSource(
                    new ByteArrayInputStream(xml.getBytes("utf-8"))));
            document.getDocumentElement().normalize();
            NodeList nodeList = document.getElementsByTagName("Program");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node nodeItem = nodeList.item(i);
                if (nodeItem.getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) nodeItem;
                    program.setProgramNumber(((org.w3c.dom.Element) element)
                            .getAttribute("programNumber"));
                    program.setImgUrl(((org.w3c.dom.Element) element)
                            .getAttribute("imgUrl"));
                    program.setDescription(((org.w3c.dom.Element) element)
                            .getAttribute("description"));
                }
            }
            nodeList = document.getElementsByTagName("Episode");
             for (int i = 0; i < nodeList.getLength(); i++) {
                 Node nodeItem = nodeList.item(i);
                 if (nodeItem.getNodeType() == Node.ELEMENT_NODE) {
                     Element element = (Element) nodeItem;
                     episode.setpN(((org.w3c.dom.Element) element)
                                .getAttribute("pN"));
                     episode.setEpisodeNumber(((org.w3c.dom.Element) element)
                            .getAttribute("episodeNumber"));
                     episode.setTransmissionName(((org.w3c.dom.Element) element)
                            .getAttribute("transmissionName"));
                     episode.setDate(((org.w3c.dom.Element) element)
                            .getAttribute("date"));
                     episode.setTime1(((org.w3c.dom.Element) element)
                            .getAttribute("time1"));
                     episode.setTime2(((org.w3c.dom.Element) element)
                            .getAttribute("time2"));
                     episode.setChannel(((org.w3c.dom.Element) element)
                            .getAttribute("channel"));
                     episode.setChannelLogo(((org.w3c.dom.Element) element)
                            .getAttribute("channelLogo"));
                 }
             }
        } catch (Exception e) {
            Log.d("XML", "Exception: " + e);
        }
    }

}
Programs.java

public class Episode {
    String pN, episodeNumber, transmissionName, date, time1, time2, channel, channelLogo;

    public String getpN() {
        return pN;
    }

    public void setpN(String pN) {
        this.pN = pN;
    }

    public String getEpisodeNumber() {
        return episodeNumber;
    }

    public void setEpisodeNumber(String episodeNumber) {
        this.episodeNumber = episodeNumber;
    }

    public String getTransmissionName() {
        return transmissionName;
    }

    public void setTransmissionName(String transmissionName) {
        this.transmissionName = transmissionName;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime1() {
        return time1;
    }

    public void setTime1(String time1) {
        this.time1 = time1;
    }

    public String getTime2() {
        return time2;
    }

    public void setTime2(String time2) {
        this.time2 = time2;
    }

    public String getChannel() {
        return channel;
    }

    public void setChannel(String channel) {
        this.channel = channel;
    }

    public String getChannelLogo() {
        return channelLogo;
    }

    public void setChannelLogo(String channelLogo) {
        this.channelLogo = channelLogo;
    }

}
public class Program {
    public Episode[] episodes;

    String programNumber, imgUrl, description;

    public Episode[] getEpisodes() {
        return episodes;
    }

    public void setEpisodes(Episode[] episodes) {
        this.episodes = episodes;
    }

    public String getProgramNumber() {
        return programNumber;
    }

    public void setProgramNumber(String programNumber) {
        this.programNumber = programNumber;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}
public class Programs {
    public Program[] programs;

    public Program[] getPrograms() {
        return programs;
    }

    public void setPrograms(Program[] programs) {
        this.programs = programs;
    }
}
import it.lucgian84.models.Episode;
import it.lucgian84.models.Program;
import it.lucgian84.models.Programs;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.renderscript.Element;
import android.util.Log;

public class XmlParser {
    private String xml;
    private Programs programs;
    private Program program = new Program();
    private Episode episode = new Episode();

    public XmlParser(String xml) {
        this.xml = xml;
    }

    public void parseXml() {
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory
                    .newDocumentBuilder();
            Document document = documentBuilder.parse(new InputSource(
                    new ByteArrayInputStream(xml.getBytes("utf-8"))));
            document.getDocumentElement().normalize();
            NodeList nodeList = document.getElementsByTagName("Program");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node nodeItem = nodeList.item(i);
                if (nodeItem.getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) nodeItem;
                    program.setProgramNumber(((org.w3c.dom.Element) element)
                            .getAttribute("programNumber"));
                    program.setImgUrl(((org.w3c.dom.Element) element)
                            .getAttribute("imgUrl"));
                    program.setDescription(((org.w3c.dom.Element) element)
                            .getAttribute("description"));
                }
            }
            nodeList = document.getElementsByTagName("Episode");
             for (int i = 0; i < nodeList.getLength(); i++) {
                 Node nodeItem = nodeList.item(i);
                 if (nodeItem.getNodeType() == Node.ELEMENT_NODE) {
                     Element element = (Element) nodeItem;
                     episode.setpN(((org.w3c.dom.Element) element)
                                .getAttribute("pN"));
                     episode.setEpisodeNumber(((org.w3c.dom.Element) element)
                            .getAttribute("episodeNumber"));
                     episode.setTransmissionName(((org.w3c.dom.Element) element)
                            .getAttribute("transmissionName"));
                     episode.setDate(((org.w3c.dom.Element) element)
                            .getAttribute("date"));
                     episode.setTime1(((org.w3c.dom.Element) element)
                            .getAttribute("time1"));
                     episode.setTime2(((org.w3c.dom.Element) element)
                            .getAttribute("time2"));
                     episode.setChannel(((org.w3c.dom.Element) element)
                            .getAttribute("channel"));
                     episode.setChannelLogo(((org.w3c.dom.Element) element)
                            .getAttribute("channelLogo"));
                 }
             }
        } catch (Exception e) {
            Log.d("XML", "Exception: " + e);
        }
    }

}
要解析XML文件,我创建了以下类:

XmlParser.java

public class Episode {
    String pN, episodeNumber, transmissionName, date, time1, time2, channel, channelLogo;

    public String getpN() {
        return pN;
    }

    public void setpN(String pN) {
        this.pN = pN;
    }

    public String getEpisodeNumber() {
        return episodeNumber;
    }

    public void setEpisodeNumber(String episodeNumber) {
        this.episodeNumber = episodeNumber;
    }

    public String getTransmissionName() {
        return transmissionName;
    }

    public void setTransmissionName(String transmissionName) {
        this.transmissionName = transmissionName;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime1() {
        return time1;
    }

    public void setTime1(String time1) {
        this.time1 = time1;
    }

    public String getTime2() {
        return time2;
    }

    public void setTime2(String time2) {
        this.time2 = time2;
    }

    public String getChannel() {
        return channel;
    }

    public void setChannel(String channel) {
        this.channel = channel;
    }

    public String getChannelLogo() {
        return channelLogo;
    }

    public void setChannelLogo(String channelLogo) {
        this.channelLogo = channelLogo;
    }

}
public class Program {
    public Episode[] episodes;

    String programNumber, imgUrl, description;

    public Episode[] getEpisodes() {
        return episodes;
    }

    public void setEpisodes(Episode[] episodes) {
        this.episodes = episodes;
    }

    public String getProgramNumber() {
        return programNumber;
    }

    public void setProgramNumber(String programNumber) {
        this.programNumber = programNumber;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}
public class Programs {
    public Program[] programs;

    public Program[] getPrograms() {
        return programs;
    }

    public void setPrograms(Program[] programs) {
        this.programs = programs;
    }
}
import it.lucgian84.models.Episode;
import it.lucgian84.models.Program;
import it.lucgian84.models.Programs;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.renderscript.Element;
import android.util.Log;

public class XmlParser {
    private String xml;
    private Programs programs;
    private Program program = new Program();
    private Episode episode = new Episode();

    public XmlParser(String xml) {
        this.xml = xml;
    }

    public void parseXml() {
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory
                    .newDocumentBuilder();
            Document document = documentBuilder.parse(new InputSource(
                    new ByteArrayInputStream(xml.getBytes("utf-8"))));
            document.getDocumentElement().normalize();
            NodeList nodeList = document.getElementsByTagName("Program");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node nodeItem = nodeList.item(i);
                if (nodeItem.getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) nodeItem;
                    program.setProgramNumber(((org.w3c.dom.Element) element)
                            .getAttribute("programNumber"));
                    program.setImgUrl(((org.w3c.dom.Element) element)
                            .getAttribute("imgUrl"));
                    program.setDescription(((org.w3c.dom.Element) element)
                            .getAttribute("description"));
                }
            }
            nodeList = document.getElementsByTagName("Episode");
             for (int i = 0; i < nodeList.getLength(); i++) {
                 Node nodeItem = nodeList.item(i);
                 if (nodeItem.getNodeType() == Node.ELEMENT_NODE) {
                     Element element = (Element) nodeItem;
                     episode.setpN(((org.w3c.dom.Element) element)
                                .getAttribute("pN"));
                     episode.setEpisodeNumber(((org.w3c.dom.Element) element)
                            .getAttribute("episodeNumber"));
                     episode.setTransmissionName(((org.w3c.dom.Element) element)
                            .getAttribute("transmissionName"));
                     episode.setDate(((org.w3c.dom.Element) element)
                            .getAttribute("date"));
                     episode.setTime1(((org.w3c.dom.Element) element)
                            .getAttribute("time1"));
                     episode.setTime2(((org.w3c.dom.Element) element)
                            .getAttribute("time2"));
                     episode.setChannel(((org.w3c.dom.Element) element)
                            .getAttribute("channel"));
                     episode.setChannelLogo(((org.w3c.dom.Element) element)
                            .getAttribute("channelLogo"));
                 }
             }
        } catch (Exception e) {
            Log.d("XML", "Exception: " + e);
        }
    }

}
import it.lucgian84.models.eposion;
导入it.lucgian84.models.Program;
导入it.lucgian84.models.Programs;
导入java.io.ByteArrayInputStream;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入org.w3c.dom.Document;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
导入org.xml.sax.InputSource;
导入android.renderscript.Element;
导入android.util.Log;
公共类XmlParser{
私有字符串xml;
私人项目;
私有程序=新程序();
私人插曲=新插曲();
公共XmlParser(字符串xml){
this.xml=xml;
}
public void parseXml(){
试一试{
DocumentBuilderFactory DocumentBuilderFactory=DocumentBuilderFactory
.newInstance();
DocumentBuilder DocumentBuilder=documentBuilderFactory
.newDocumentBuilder();
Document Document=documentBuilder.parse(新输入源(
新的ByteArrayInputStream(xml.getBytes(“utf-8”);
document.getDocumentElement().normalize();
NodeList NodeList=document.getElementsByTagName(“程序”);
for(int i=0;i
我不知道如何将对象片段插入阵列程序,将对象程序插入阵列程序。 我希望你能帮我找到解决这个问题的办法。 多谢各位

  • 我个人更喜欢另一种方法,因为它更多的是面向对象的范例,更容易阅读(至少对我来说),更容易应对未来的变化

  • 代码如下所示:

  • 对象
    程序
    插曲
    被实例化
  • 对象
    程序
    插曲
    已填充
  • 对象
    节目
    剧集
    将添加到列表中
  • 可以使用
    list.get(index)
    从列表中访问对象,并且可以在列表中更改对象,如:
    ((程序)list.get(index)).setDescription(“我的新描述”)
    ,列表将被更新

  • 我个人更喜欢另一种方法,因为它更多的是面向对象的范例,更容易阅读(至少对我来说),更容易应对未来的变化

  • 代码如下所示:

  • 对象
    程序
    插曲
    被实例化
  • 对象
    程序
    插曲
    已填充
  • 对象
    节目
    剧集
    将添加到列表中
  • 可以使用
    list.get(index)
    从列表中访问对象,并且可以在列表中更改对象,如:
    ((程序)list.get(index)).setDescription(“我的新描述”)
    ,列表将被更新


只是一个问题:在将所有信息插入对象后,我应该执行的最后两条指令是什么?我将插入这个
programsList.add(newprogram())在我将信息插入到目标程序和这个
programList.add(新插曲())之后在我将信息插入对象后。是吗?@PaoloRobertetti:你说得很对。首先要做的是将信息插入到剧集中,然后将剧集集合对象插入到节目中,最后将节目对象插入到节目集合中。就这样