Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Dynamic Array

I am parsing an XML file through Android Pull Parser technique. First, have a look at the below XML file:

<childs>
    <students>
        <name> hello </name>
        <address> xyz </address>
    </stdents>      

    <students>
        <name> abc </name>
        <address> def </address>
    </stdents>      

</childs>

Consider that I'm parsing the above file. Now, my problem is that I want to create a separate array for name and address. So while parsing, I want to store 1st student's data in name[0] and address[0] and the next student's data in name[1] and address[1]. In short, array size is extending as more data is parsed.

Is there any way to do so? I mean to create a dynamic extendable array? Or if there is another way to do so then please help me to fight with this problem.

like image 890
Paresh Mayani Avatar asked Aug 11 '10 09:08

Paresh Mayani


2 Answers

You could use Vector<String> and then (if you need an array) copy the data to array(s) using toArray method.

    Vector<String> v = new Vector<String>();
    for (int i=0; i<10; i++)
        v.add(new String(Integer.toString(i)));

    Object s[] = v.toArray();

    for(int i=0; i<10; i++)
        str = s[i].toString();

Another option:

    String a[] = {};
    v.toArray(a);
like image 174
Asahi Avatar answered Nov 15 '22 04:11

Asahi


You use and List array like this

List<String> name=new ArrayList<String>();
List<String> address=new ArrayList<String>(); 
name.add(StudyParser.getValue(eimg, "name"));  
address.add(StudyParser.getValue(eimg,"address")

where StudyParser.getValue(); is method which u call to get data

like image 31
Khan Avatar answered Nov 15 '22 04:11

Khan