Android - How to set value of ArrayList to TextView - android

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
please help me, and sorry for my english >_<

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");
}

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");
}

You never initialize your array... objPlace[] is a null array.
You need to do something like:
objPlace[] = new Object[arraysizenumber]

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.

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 :)

Related

Android shuffle Questions and Answers (string arrays)

I am making an app in which there are list of questions and respective answers.
Questions are in one string array, while answers are in another string array.
I have implemented the following in a wish to shuffle the questions. (Of course the answers need to be linked to that question, else meaningless)
Code:
selected_Q = new String[totalnoofQ];
selected_A = new String[totalnoofQ];
int[] random_code = new int[totalnoofQ];
for (int i = 0; i < totalnoofQ; i++)
{
random_code[i] = i;
}
Collections.shuffle(Arrays.asList(random_code));
for (int j = 0; j < totalnoofQ; j++)
{
int k = random_code[j];
selected_Q [j] = databank_Q [k];
selected_A[j] = databank_A [k];
}
The code reports no fatal error, but the selected_Q is still in sequential order. Why?
Could you please show me how can I amend the codes? Thanks!!!
You shuffle a list created using random_code, but random_code is not modified.
You need to create a temporary list based on random_code. Shuffle this list and then use it to fill the selected_X arrays.
Something like this should work :
int[] random_code = new int[totalnoofQ];
for (int i = 0 ; i < totalnoofQ ; i++) {
random_code[i] = i;
}
List<Integer> random_code_list = new ArrayList<Integer>(); // Create an arraylist (arraylist is used here because it has indexes)
for (int idx = 0 ; idx < random_code.length ; idx++) {
random_code_list.add(random_code[idx]); // Fill it
}
Collections.shuffle(random_code_list); // Shuffle it
for (int j = 0 ; j < totalnoofQ ; j++) {
int k = random_code_list.get(j); // Get the value
selected_Q[j] = databank_Q[k];
selected_A[j] = databank_A[k];
}

Android - KML Parser using DOM Parser

i've a problem here.
i wanna parsing a KML document from google maps. i'd successfuly to parsing the nodes which stores a coordinate.
but i wanna parse the node as childnodes
here is the KML documents.
<Placemark>
<name>Head north on Jalan Kebagusan Raya toward Jalan Anggrek</name>
<description><![CDATA[go 850 m]]></description>
<address>Jalan Kebagusan Raya</address>
...
</Placemark
to parse it i used this code,
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());
}
}
desk.setText("");
for (int j = 0; j < strPlace.length; j++){
desk.append("Deskripsi:\n" + strPlace[j] + "\n");
}
but when i tried in a real device/emulator, i got something i dont want to.
this is the screenshot from my real device
from the scrshot, i just want to grab the "Head northeast on Jalan Darmo Kali toward Jalan Darmoerjo 4" informations. just it.
can u help me to solve my problems?
thanks in advance, and sorry for my english, thanks :)
Something like the following should do what you want.
NodeList nlPlace = doc.getElementsByTagName("Placemark");
for(int p = 0; p < nlPlace.getLength(); p++){
Element place = (Element)nlPlace.item(p);
Element name = (Element)place.getElementsByTagName("name");
//.....
}
}

android JSONArray length

i have an array from a json feed i know that in the jArray there is one league but i need to work out the count of that array incase a second is added to the feed at a later date. at the moment log cat isn't logging out "teamFeedStructure" does anyone know what I'm doing wrong or know how to correctly turn the length of an array into a string that i can use in an intent?
heres the code
JSONObject obj = new JSONObject(json);
JSONObject objData = obj.getJSONObject("data");
JSONArray jArray = objData.getJSONArray("structure");
//String leagues = jArray.toString();
//Log.v("myapp", "structure" + leagues);
for (int i=0; i < jArray.length(); i++) {
JSONObject oneObject = jArray.getJSONObject(i);
leaguecountArray = oneObject.getJSONArray("league_id");
for (int l=0; l < leaguecountArray.length(); l++) {
if (leaguecountArray.length() == 1) {
teamFeedStructure = "One";
}
if (leaguecountArray.length() == 2) {
teamFeedStructure = "Two";
}
Log.v("myapp", teamFeedStructure);
}
}
Why do you need to iterate here:
for (int l=0; l < leaguecountArray.length(); l++)
{
if (leaguecountArray.length() == 1){
teamFeedStructure = "One";
}
if (leaguecountArray.length() == 2){
teamFeedStructure = "Two";
}
Log.v("myapp", teamFeedStructure);
}
Nevermind how many passes you do the result will still be the same.
Also you can use not English words, but String holding the number you need. Do like that:
teamFeedStructure = String.valueOf(leaguecountArray.length());
Then the value will become "2" for example. In your other activity you can parse it again to integer like that: int number = Integer.parseInt(teamFeedStructure);
Add the following line
teamFeedStructure = "greater two";
before
if (leaguecountArray.length() == 1){
If you get greater two message, means your array length more than two.
Thanks for your help everyone i discovered that i had gone a step too far into the feed which is why i was not getting anything in Logcat. now it's counting fine.

Matrix initialization in Android?

I'm tring to do a simple matrix initialization in android and I get Error: java.lang.ArrayIndexOutOfBoundsException. I'm trying this:
Integer id = Integer.valueOf(idcateg);
System.out.println("Id-ul e"+id);
vot = new Integer[sirid.length][id];
for (int i = 0; i < sirid.length; i++) {
vot[i][id] = 0;
}
where id is a value between 1 and 5,sirid.length is a number that reflects number of images from different categorys. For example,I want for category 1 to have something like this :
vot[0][1]=0;
vot[1][1]=0;
vot[2][1]=0;
...etc
Where is my mistache?
try this
Integer id = Integer.valueOf(idcateg);
System.out.println("Id-ul e"+id);
vot = new Integer[sirid.length][id];
for (int i = 0; i < sirid.length; i++) {
vot[i][id-1] = 0;
}
array index start from 0
you set the size of the vot array to sirid.lengh by id but the array start index value from 0 to (size)(size value not included) see the for loop
I think because you initialize the array on id.
After that you call to vot[i][id] which then will alway be 1 too high.
f.e.
if you create new int[3][3]
you can only call positions 0,1 and 2
good luck
That is because id has been considered as Length of the String and you r trying to access the same element... but the last element is vat[i][id-1] but u r trying to get vat[i][id]
So better use this..
for (int i = 0; i < sirid.length; i++) {
for(int j = 0; j<id ; j++){
vot[i][j] = 0;
}
}

Android Array String

Hi guys if i try to assign the string to string Array i am getting NULL POINTER EXCEPTION..
Pls give me a solution..
Here is my code and i am getting null pointer in this line thumbs[j]=title;
as code Follows...
for (int i = 0; i < items.getLength(); i++) {
Node item = items.item(i);
NodeList properties = item.getChildNodes();
for (int j = 0; j < properties.getLength(); j++) {
Node property = properties.item(j);
String name = property.getNodeName();
if (name.equalsIgnoreCase("title")) {
try{
property.normalize();
Spanned title2 = Html.fromHtml(property.getFirstChild().getNodeValue().replaceAll("\\<.*?>", ""));
title = title2.toString();
}catch(Exception e){
Log.v("Exception",""+e);
}
thumbs[j]=title;
}
}
}
My guess is that the thumbs[] hasn't been initialized doing something like:
String [] thumbs = new String [properties.getLength()];
Though it's hard to say - your code doesn't show your declaration of this array.
The only possibility is that thumbs is null. It isn't a compile error if you don't initialize it. Don't rely on the IDE to tell you if something initialized prove it to yourself by examining your code, or print out the value of thumbs before you use it every time.
The only other exception possibilities at that line are ArrayIndexOutOfBoundsException if j > thumbs.length-1. But again, that's not a NPE. If title was null, that's fine and not an exception.

Categories

Resources