DevYudh
DevYudh

Reputation: 2737

Android - How to set value of ArrayList to TextView

i have a problem here. I've ArrayList from the result of my parser class. then i wanna put that value (value from ArrayList) to TextView.

here is the work i've done till now. I create TextView on my main.xml

<TextView
        android:id="@+id/deskripsi" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
    />

and here at my onCreate Method, i initialized the Text View

desk = (TextView)findViewById(R.id.deskripsi);

and then i tried to parser the KML document from google map and at the Node Placemark i put its value to ArrayList, here my code

ArrayList<String> pathName = new ArrayList<String>();
Object[] objPlace;

//Parser Node Placemark
NodeList nlPlace = doc.getElementsByTagName("Placemark");
        for(int p = 0; p < nlPlace.getLength(); p++){
            Node rootPlace = nlPlace.item(p);
            NodeList itemPlace = rootPlace.getChildNodes();
            for(int y = 0; y < itemPlace.getLength(); y++){
                Node placeStringNode = itemPlace.item(y);
                NodeList place = placeStringNode.getChildNodes();
                //valueName = nameList.item(0).getNodeValue().toString() + "+";
                pathName.add(place.item(0).getNodeValue());
            }
    }

objPlace = pathName.toArray();

desk.setText("");
for (int j = 0; j < objPlace.length; j++){
    desk.append("Deskripsi:\n" + objPlace[j].toString() + "\n");
}

but when itried to run its to my emulator and real device, i get an error. here's my LogCat enter image description here

please help me, and sorry for my english >_<

Upvotes: 0

Views: 19956

Answers (5)

DevYudh
DevYudh

Reputation: 2737

Thanks my fren. im still not really well on using Object[]. then i used this code for solving my problem

String strPlace[] = (String[])pathName.toArray(new String[pathName.size()]);

desk.setText("");
        for (int j = 0; j < strPlace.length; j++){
            desk.append("Deskripsi:\n" + strPlace[j] + "\n");
        }

Thanks for userSeven7s, flameaddict and khan for trying to help me. nice to know u all :)

Upvotes: 0

Khan
Khan

Reputation: 7605

you can put the line

 objPlace[] = new Object[pathName.size()] 

before

objPlace = pathName.toArray();

and check output otherwise do this way

 desk.setText("");

 for (int j = 0; j < pathName.size(); j++){
     desk.append("Deskripsi:\n" + pathName.get(j) + "\n");
  }

Upvotes: 1

flameaddict
flameaddict

Reputation: 64

Ii is caused by NullPointerException, that means some needs parameters and you have passed a null value to it. Based on your code it might be around desk.setText("");. Try inserting a space or some value into it and run.

Upvotes: 0

Barak
Barak

Reputation: 16393

You never initialize your array... objPlace[] is a null array.

You need to do something like:

objPlace[] = new Object[arraysizenumber]

Upvotes: 0

Ron
Ron

Reputation: 24235

You can do this instead; use pathName directly in the for loop...

desk.setText("");
for (int j = 0; j < pathName.size(); j++){
    desk.append("Deskripsi:\n" + pathName.get(j) + "\n");
}

Upvotes: 10

Related Questions