简体   繁体   中英

Java String formation for ListView

I have created a Java code for my Android App.

String[] MovieName=new String[]{};

for (int i = 0; i < 15; i++) 
{
MovieName[i]=movieAtt.getAttributeValue( "name" );  //Value coming from my XML
}

ListViewObject.setAdapter(new ArrayAdapter<String>(screen2.this,android.R.layout.simple_list_item_1 , MovieName));

This code throws an Exception. I think i am not inserting vaues properly inside Java String Array.

All i want is to have a variable like MovieName={"1","2", "3"} to feed into the ListView of my code.

This is not much helpful too : http://download.oracle.com/javase/6/docs/api/java/lang/String.html

Your initilizing an empty string array. That will give you an ArrayOutOfBoundException.

If you always have 15 entries you could initialize it to 15.

String[] MovieName=new String[15];

Otherwise you could create an ArrayList and convert it to an array after you filled it.

You initialize an empty array.

Try this

String[] MovieName = new String[15];

If number of elements in MovieName is constant, then you should initialise it as

String[] MovieName=new String[15];

Your current initialisation is equal to

String[] MovieName=new String[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM