SPB
SPB

Reputation: 4218

how to display a list in android

I have a String countries[].

now when i click a button then on the onClick event this abc array is filled.suppose it is filled with the name of 10 countries.These 10 countries should be visible as a list so that i can choose any 1 country among the list. but i am not able to show them as a list.

My programme crashes if i use the following code :

ListView list = (ListView)findViewById(R.id.ListView);

list.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, countries));

As countries is filled later on the click of a button.So initially the countries array is empty and as the onCreate() is executed first it crashes.

I found the examples on net where there is pre-defined array.so it works.My array is filled in a onClick event.how can i display List at that moment???

Upvotes: 0

Views: 1569

Answers (1)

st0le
st0le

Reputation: 33545

Make list your member variable.

Move the setAdapter call inside your onClick()....That'll do.

Upvotes: 1

Related Questions