Amount allocated to the variable within the setOnClickListener is not being considered

0

So I'm doing a test app more to study myself, I'm a beginner on android do not know much, I'm trying to get it in the application when I click on an image that it goes into 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 it enters into this method, it does not assign value to the position variable, I've tried other ways I declare another variable there and made the position receive what was inside more nor does it work.

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

The variable position would be the variable that would define the value of the if below, so that when I declare for example position = 1, that is, position gets one out of function it works without problems, plus inside method p1 that is the left image is as if the position = 1 was not even typed there.

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

publicclassFragment_mainextendsFragment{publicFragment_main(){}privateintposicao=0;@OverridepublicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){finalViewAcess=inflater.inflate(R.layout.fragment_main,container,false);ImageViewp1=(ImageView)Acess.findViewById(R.id.p1);ImageViewp2=(ImageView)Acess.findViewById(R.id.p2);p1.setOnClickListener(newImageView.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;
    }


}
    
asked by anonymous 14.10.2016 / 15:37

1 answer

2

The code that is in the listener only runs when an image is clicked. So when the run arrives on the line

if (posicao==1)

The value of the variable posicao is zero.

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

If I understand correctly what you want it 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;
    }
}
    
14.10.2016 / 16:05