开发者

Build error for ArrayAdapter (Android, noob)

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开发者_StackOverflow社区,
                                          todoArrayList);

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

Thanks in advance!!


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);


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜