Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – How to change a variable from an activity inside my adapter (java class change variable passed to it)

Posted on November 11, 2022 By

Solution 1 :

You can make SoundPageChangeListener inner class of your activity, so the Listener can access to the Activity selectedToe variable.
Having an inner class will allow this class SoundPageChangeListener to have visibility and access to your activity selectedToe variable.

The pointer is not kept in your scenario as you are creating a new class instance. Your are assigning currentSound not re-pointing to the memory address.

Then in onPageSelected assign selectedToe with sound


class YourActivity {
    ...
    Sound selectedToe = currentSound // set the default sound if any

    // Inside the activity, declare your listener
    public class SoundPageChangeListener implements ViewPager.OnPageChangeListener {

        private ArrayList<Sound> sounds;

        // only the sound list is required here
        public SoundPageChangeListener(ArrayList<Sound> sounds) {
            this.sounds = sounds;
        }

        @Override
        public void onPageSelected(int position) {
            Sound sound = sounds.get(position);
            YourActivity.this.selectedToe = sound
        }
    }
}

Problem :

My Activity has the value Sound selectedToe. I want my soundPageChangeListener to be able to update the selectedToe of the activity, so I pass selectedToe like this:

SoundPageChangeListener soundPageChangeListener = new SoundPageChangeListener(soundsToe, selectedToe);

however, when I change selectedToe inside soundPageChangeListener:

public class SoundPageChangeListener implements ViewPager.OnPageChangeListener {

    private ArrayList<Sound> sounds;
    Sound currentSound;

    public SoundPageChangeListener(ArrayList<Sound> sounds, Sound currentSound) {
        this.sounds = sounds;
        this.currentSound = currentSound;
    }

    @Override
    public void onPageSelected(int position) {
        Sound sound = sounds.get(position);
        currentSound = sound;
    }

the selectedToe on the activity still null.

Aren’t all java values pointers? If I change something inside the listener, shouldn’t it change outside?

READ  [FIXED] android - How to save the UID of a user on registering the user using firebase Auth
Powered by Inline Related Posts
Android Tags:android, java

Post navigation

Previous Post: [FIXED] java – Access Binding Adapters in multi module project
Next Post: [FIXED] java – Changing value in on item also changes in another item – RecyclerView Android

Related Posts

[FIXED] c# – How to enable or sent whatsapp message in xamarin for android Android
[FIXED] android – kotlin findViewById returns null Android
[FIXED] android – NativeScript Vue – Only render items in viewport on the list Android
[FIXED] android – Samsung galaxy S10+ Crash report on Play Store Android
[FIXED] java – How does AsyncTask work one process to another one? Android
[FIXED] android – Track push notifications and log them Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme