String Array作为Java / Android中的返回值

时间:2012-09-14 08:57:26

标签: android xml string arraylist sax

我正在使用SAX XML Parser从XML文件中获取值,该文件工作正常。这是我主要活动中的方法:

public void parseXML() {

    String parsedData = "";

    try {

        Log.w("AndroidParseXMLActivity", "Start");
        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        XMLContentHandler myXMLHandler = new XMLContentHandler();
        xr.setContentHandler(myXMLHandler);

        AssetManager assetManager = getAssets();
        InputStream inputStream = null;
        try {
            inputStream = assetManager.open("calimero.xml");
        } catch (IOException e) {
            Log.e("tag", e.getMessage());
        }
        xr.parse(new InputSource(inputStream)); 

        ArrayList<Datapoint> itemsList = myXMLHandler.getItemsList();
        int datapoint_size = itemsList.size();
        String xml_names[]  = new String [datapoint_size];
        String xml_states[]  = new String [datapoint_size];
        String xml_dptIDs[] = new String [datapoint_size];
        String xml_groupadresses[]  = new String [datapoint_size];
        String xml_mainNumbers[]  = new String [datapoint_size];

        for(int i=0;i<itemsList.size();i++){
            Datapoint item = itemsList.get(i);
            parsedData = parsedData + "----->\n";
            parsedData = parsedData + "Name: " + item.getName() + "\n";
            xml_names[i] = item.getName();
            parsedData = parsedData + "stateBased: " + item.getStateBased() + "\n";
            xml_states[i] = item.getStateBased();
            parsedData = parsedData + "mainNumber: " + item.getMainNumber() + "\n";
            xml_mainNumbers[i] = item.getMainNumber();
            parsedData = parsedData + "dptID: "+ item.getDptID() + "\n";
            xml_dptIDs[i] = item.getDptID();
            parsedData = parsedData + "Groupadress: "+ item.getGroupadress() + "\n";
            xml_groupadresses[i] = item.getGroupadress();
        }
        Log.w("AndroidParseXMLActivity", "Done");
    }
    catch (Exception e) {
        Log.w("AndroidParseXMLActivity",e );
    }
    Log.w("AndroidParseXMLActivity", names[0]);
    Log.w("AndroidParseXMLActivity", mainNumbers[2]);
    Log.w("AndroidParseXMLActivity", dptIDs[3]);
    //xmlOutput.setText(parsedData);
}

我想在我的方法之外使用name,mainNumber等值。但我不明白......我知道这是java基础.... 请帮助我,谢谢!

2 个答案:

答案 0 :(得分:1)

如果要在整个类中使用String数组,则必须在类中定义它。然后该变量可用于您班级中的所有功能。

    public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        StringArray testString = new StringArray();
        testString.setArray();
        testString.printArray();
    }

    static private class StringArray{
        String test[];

        public void setArray(){
            test = new String[5];
            for (int i = 0 ; i < test.length; i++){
                test[i] = "test";
            }
        }

        public void printArray(){
            for (int i = 0 ; i < test.length; i++){
                System.out.println(test[i]);
            }
        }
    }
}

答案 1 :(得分:0)

我解决了这个问题。我现在使用Datapoint作为返回值:

public Datapoint parseXML(){
    Datapoint item = null;

    for(){
        ...
    }

    return item;
}

然后在我的主要功能中我可以使用

item = parseXML();
String name = item.getName();