0

I have a class within an Activity called Pdj . . .

        private class ButtonListener implements View.OnClickListener {

        public void onClick(View v) {
            ListView myListView = (ListView)findViewById(R.id.pdjListView);
            EditText screen2EditText = (EditText)findViewById(R.id.pdjEditText);        
            ArrayList<String> todoArrayList = new ArrayList<String>();                
            // array adapter to bind the array to the list view
            ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
                                          android.R.layout.simple_list_item_1,
                                          todoArrayList);
       . . . 

The last line generates the build error "The constructor ArrayAdapter(Pdj.ButtonListener, int, ArrayList) is undefined"

Someone else on StackOverflow reported this problem an was told to try . . .

                ArrayAdapter<String> aa = new ArrayAdapter<String>(this.getContext(),
                                          android.R.layout.simple_list_item_1,
                                          todoArrayList);

... but that just produces "The method getContext() is undefined for the type Pdj.ButtonListener"

Thanks in advance!!

2 Answers 2

3

please see the comments start with "##" in your snippet.

private class ButtonListener implements View.OnClickListener {

        public void onClick(View v) {
            ListView myListView = (ListView)findViewById(R.id.pdjListView);
            EditText screen2EditText = (EditText)findViewById(R.id.pdjEditText);        
            ArrayList<String> todoArrayList = new ArrayList<String>();                
            // array adapter to bind the array to the list view

            // ## "this" refers to class ButtonListener, obviously, it's not a context
            // ## so the compile error raised.
            ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
                                          android.R.layout.simple_list_item_1,
                                          todoArrayList);
       . . . 

try to change your code like the format below:

ArrayAdapter<String> aa = new ArrayAdapter<String>(YourActivity.this,
                                              android.R.layout.simple_list_item_1,
                                              todoArrayList);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! As a Java/Android noob there's obviously something about context I'm not getting. Since ButtonListener is defined and runs within my Activity class why doesn't it get the context from the enclosing class?
. . . Not only that but the fix was to say "Pdj.this". But in the error message "The constructor ArrayAdapter(Pdj.ButtonListener ..." it implies that the compiler already recognized that the current class is part of Pdj (my Activity class). So even though my basic question is answered I'm still a bit confused.
0

Whatever Activity class this is in try calling TheActivityClassName.this.getContext()

Comments

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.