0

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 Answer 1

1

Make list your member variable.

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

Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.