0

I have a misunderstanding of how to dissect this error message. I know there are simpler and more elegant ways of doing this but I just wanted to do it this way to aid understanding. We are making an app where the seekbar is used to complete a times table. Sorry to dump a bunch of code but I just cant understand what it is trying to tell me.

We already made an audio player seekbar and an arraylist for family members names. I am trying to use that limited experience to create this app. Why is this not working?

The simpler the explanation the better. At this stage it is impossible to be patronising.

Ive read similar questions but its not particular to this problem. For example cannot resolve constructor 'arrayadapter(android.widget.adapterview.onitemselectedlistener, int, java.util.arraylist)'

Here is my code:

//Define Listview and Seekbar in code
        final ListView listView = (ListView) findViewById(R.id.myListView);
        SeekBar seekBar = (SeekBar) findViewById(R.id.mySeekBar);

        //Set max and min Seekbar and get int
        seekBar.setMax(20);
        seekBar.setMin(1);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                //Create Arraylist
                ArrayList<Integer> arrayList = new ArrayList<>();
                for (int i = 1; i < 15; i++){
                arrayList.add(i * progress);

                    //create adapter and set listview
                    ArrayAdapter arrayAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_list_item_1, arrayList);
                    listView.setAdapter(arrayAdapter);

            }
hulahoops
  • 17
  • 5

2 Answers2

0

You are using incorrect context as while creating ArrayAdapter as this in below code will refer to SeekBar and not Activity. Use YourActivity.this instead of using only this.

 ArrayAdapter arrayAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_list_item_1, arrayList);
karan
  • 8,637
  • 3
  • 41
  • 78
0

new SeekBar.OnSeekBarChangeListener is an anonymous inner class and calling this inside the class references to the SeekBar.OnSeekBarChangeListener class instead of your Activity class.

The first parameter here for the ArrayAdapter is expected to be a Context, but in your case you are passing your OnSeekBarChangeListener instead. So you need to use Activity.this rather than this.

Ahmadul Hoq
  • 705
  • 5
  • 14
  • Many thanks. During the course we always used "this" without explanation. I appreciate the time you invested in answering. Sincere thanks once more. – hulahoops May 16 '19 at 09:32
  • getApplicationContext() did it for me. From here:https://stackoverflow.com/questions/3572463/what-is-context-on-android – hulahoops May 16 '19 at 09:56
  • @hulahoops Glad to help! Do note that `getApplicationContext()` refers to the `Context` of your application, instead of `Activity`. So creating `ArrayAdapter` using the application context will cause the adapter to be alive until your application is alive. It's better you use `Activity` context for this, so that your adapter is destroyed and garbage collected when your activity is destroyed. If you are unable to access the activity context using `YourActivityName.this`, you can use `seekBar.getContext()`. – Ahmadul Hoq May 17 '19 at 10:35