views:

33

answers:

1

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???

+1  A: 

Make list your member variable.

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

st0le
If i move setAdapter inside onClick it gives error: The constructor ArrayAdapter<String>(new View.OnClickListener(){}, int, String[]) is undefined
SPB
`this` should be transformed into `YourActivity.this`...Since your scope is within `OnClickListener`, `this` refers to its instance and not the activity's.
st0le
Thanks a lot buddy.....It really helped
SPB