Value attached to variable within setOnClickListener is not being considered

Asked

Viewed 185 times

0

So I’m making a test app more to study even, I’m beginner on android do not know much, I’m trying to make in the application when I click on an image that it enters this method :

p1.setOnClickListener(new ImageView.OnClickListener() {

            @SuppressWarnings("ResourceType") 
            public void onClick(View arg0) { 
                posicao =1;
                Toast.makeText(getContext(), "Imagem 1 selecionada!", Toast.LENGTH_SHORT).show();

            }});

I’m wondering why when he enters this method, he does not assign value to the variable position, I have tried other way declared another variable there and made the position receive what was inside but it doesn’t work like this.

These are the images this code above is from the first image, the action when you click on the image and says that it was selected..

The variable position would be the variable that would define the value of if down there, so much so that when I declare for example position = 1 , ie position gets one out of the function it works smoothly, further inside the P1 method which is the left image is as if the position = 1 was not typed there.

at the end of the post has my complete code...

aplicação no celular

aplicação no celular

public class Fragment_main extends Fragment {

    public Fragment_main() {
    }

    private int posicao=0;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       final View Acess = inflater.inflate(R.layout.fragment_main, container, false);

        ImageView p1 = (ImageView)Acess.findViewById(R.id.p1);
        ImageView p2 = (ImageView)Acess.findViewById(R.id.p2);


        p1.setOnClickListener(new ImageView.OnClickListener() {
            @SuppressWarnings("ResourceType")
            public void onClick(View arg0) {
                posicao =1;
                Toast.makeText(getContext(), "Imagem 1 selecionada!", Toast.LENGTH_SHORT).show();

            }});

        p2.setOnClickListener(new ImageView.OnClickListener() {
            @SuppressWarnings("ResourceType")
            public void onClick(View arg0) {
                Toast.makeText(getContext(), "Imagem 2 selecionada!", Toast.LENGTH_SHORT).show();
                posicao =2;

            }});

       // position =1;

        // pegando o valor que recebeu lá ....
                if (posicao==1) {

            Button caso1button = (Button) Acess.findViewById(R.id.bts); // botão

            //ImageView imagePreview = (ImageView)acessR.findViewById(R.id.viewimg); // idimg
            //imagePreview.setImageResource(R.drawable.colorpass); // local da img original

            caso1button.setOnClickListener(new Button.OnClickListener() {
                @SuppressWarnings("ResourceType")
                @Override
                public void onClick(View arg0) {

                    WallpaperManager myWallpaperManager
                            = WallpaperManager.getInstance(getContext());
                    try {
                        myWallpaperManager.setResource(R.drawable.gpx17); // wallpaper

                        Toast.makeText(getContext(), "Wallpaper aplicado com Sucesso!", Toast.LENGTH_SHORT).show();
                    } catch (IOException e) {

                        e.printStackTrace();
                    }
                }
            });
            posicao = 0;

        }else if (posicao==2) {

            Button caso2button = (Button) Acess.findViewById(R.id.bts); // botão

            //ImageView imagePreview = (ImageView)acessR.findViewById(R.id.viewimg); // idimg
            //imagePreview.setImageResource(R.drawable.colorpass); // local da img original

            caso2button.setOnClickListener(new Button.OnClickListener() {
                @SuppressWarnings("ResourceType")
                @Override
                public void onClick(View arg0) {

                    WallpaperManager myWallpaperManager
                            = WallpaperManager.getInstance(getContext());
                    try {
                        myWallpaperManager.setResource(R.drawable.gpx15); // wallpaper

                        Toast.makeText(getContext(), "Wallpaper aplicado com Sucesso!", Toast.LENGTH_SHORT).show();
                    } catch (IOException e) {

                        e.printStackTrace();
                    }
                }
            });
            posicao = 0;
        }

        return Acess;
    }


}

1 answer

2


The code that is in Listener is only executed when an image is clicked.
So when the execution reaches the line

if (posicao==1)

the value of the variable posicao is zero.

What you should do is have the code that uses Wallpapermanager in a separate method and then do the setResource() according to the value of posicao.

If I understand correctly what you want will be something like this:

public class Fragment_main extends Fragment {

    public Fragment_main() {
    }

    private int posicao=0;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       final View Acess = inflater.inflate(R.layout.fragment_main, container, false);

        ImageView p1 = (ImageView)Acess.findViewById(R.id.p1);
        ImageView p2 = (ImageView)Acess.findViewById(R.id.p2);


        p1.setOnClickListener(new ImageView.OnClickListener() {
            @SuppressWarnings("ResourceType")
            public void onClick(View arg0) {
                posicao =1;
                Toast.makeText(getContext(), "Imagem 1 selecionada!", Toast.LENGTH_SHORT).show();

            }});

        p2.setOnClickListener(new ImageView.OnClickListener() {
            @SuppressWarnings("ResourceType")
            public void onClick(View arg0) {
                Toast.makeText(getContext(), "Imagem 2 selecionada!", Toast.LENGTH_SHORT).show();
                posicao =2;

            }});

        Button caso1button = (Button) Acess.findViewById(R.id.bts); // botão
        caso1button.setOnClickListener(new Button.OnClickListener() {
            @SuppressWarnings("ResourceType")
            @Override
            public void onClick(View arg0) {

                changeWallpaper();
            }
        });


        return Acess;
    }
}

private void changeWallpaper(){

    WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getContext());
    if (posicao==1) {
        try {
            myWallpaperManager.setResource(R.drawable.gpx17); // wallpaper

            Toast.makeText(getContext(), "Wallpaper aplicado com Sucesso!", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {

            e.printStackTrace();
        }
        posicao = 0;

    }else if (posicao==2) {
        try {
            myWallpaperManager.setResource(R.drawable.gpx15); // wallpaper

            Toast.makeText(getContext(), "Wallpaper aplicado com Sucesso!", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {

            e.printStackTrace();
        }
        posicao = 0;
    }
}
  • 1

    Oops Brawl, it worked great, I didn’t think I’d have to do just that.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.