0

I want to make a function in which if a button on a different activity is pressed, then another function which is outside of this one will be invoked. But in case that the button is not pressed the function must not take place.

Here I leave you the code of what I've done so far(java.file:

public class Comida2 extends AppCompatActivity implements Adaptador2.OnRecipeListener {
    private RecyclerView recyclerView1;
    List<Entidad2> listItems;
    Adaptador2 adaptor;
    private Entidad2 entidad1,entidad2,entidad3;
    Button cambiarmenu;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_comida);

        cambiarmenu = (Button) findViewById(R.id.boton_cambiarmenu);

        recyclerView1 = findViewById(R.id.lv_1);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView1.setLayoutManager(layoutManager);

        listItems = new ArrayList<>();
        entidad1 = new Entidad2(R.drawable.calabacines_3, "Solomillo a la plancha", " 10 min.", 4, 20);
        entidad2 = new Entidad2(R.drawable.patatas_deluxe_especiadas_70523_300_150, "Entrecot", " 15 min.", 2, 50);
        entidad3 = new Entidad2(R.drawable.tomate, "Hamburguesa", " 2 min.", 5, 100);


        listItems.add(entidad1);
        listItems.add(entidad2);
        listItems.add(entidad3);

        adaptor = new Adaptador2(listItems, this);
        recyclerView1.setAdapter(adaptor);
        adaptor.notifyDataSetChanged();
        //Clicado();
    }

    @Override
    public void OnRecipe(int priority) {

        if (priority == 20) {
            Intent in = new Intent(this, Solomillo.class);
            startActivity(in);
        }
        if (priority == 50) {
            Intent in = new Intent(this, Entrecot.class);
            startActivity(in);
        }
        if (priority == 100) {
            Intent in = new Intent(this, Hamburguesa.class);
            startActivity(in);
        }
    }

    private void Clicado(){

        final boolean[] numerillo = new boolean[1];

        cambiarmenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numerillo[0] = true;
            }
        });

            if (numerillo[0]) {
            pickEntidad();
        }
    }

    private void pickEntidad(){
        final int random = new Random().nextInt(101);

        int priority1 = entidad1.getPriority();
        int priority2 = entidad2.getPriority();
        int priority3 = entidad3.getPriority();


        listItems.clear();
        if(random < priority1){

            listItems.add(entidad1);

        }else if(random < priority2){

            listItems.add(entidad2);

        }else if (random <= priority3){

            listItems.add(entidad3);

        }
        adaptor.notifyDataSetChanged();
    }
}

I want that if the button cambiarmenu is pressed, then the function pickEntidad is invoked, but that if its not, then nothing happens. I've had the problem that I had put the method in which if the button is pressed... inside the Oncreate, and then when it was not pressed the app crashed.

The method Clicado is not coded correctly as it is the method I tried to do myself in which I've included the if and the onClickListener.

Any idea of how to do it please? Thanks.

MohanKumar
  • 960
  • 10
  • 26
  • if i understand correctly, you have a button in one activity that when pressed, you want to call a function from another class? – letsCode Oct 01 '19 at 18:41
  • what's the relationship between these two activities, for example does one start the other? – Joni Oct 01 '19 at 19:43
  • Yes that's it, any idea of how to do it? @soldfor – cristiano ronaldo Oct 02 '19 at 16:32
  • The way I see it you have only 1 Activity/class but with multiple functions/methods(check your terminology). If you do have different different activity can you post it here. – L2_Paver Oct 03 '19 at 06:36
  • try directly calling pickEntidad(); inside cambiarmenu.setOnClickListener and remove the numerillo – L2_Paver Oct 03 '19 at 06:44

1 Answers1

0

I did see your another post and now I understand your problem.

"The button called cambaiarmenu is in another activity, not in Comida2" - what you said here Why can't I use a button which is not in the same activity as the java.file?

The moment you called

    setContentView(R.layout.activity_comida);

you can only declare all widgets(button ,textview, etc) under that layout and which I believe R.id.boton_cambiarmenu(cambiarmenu button id) does not belong on that layout.

So you need to declare your cambiarmenu button on the activity where you declare its layout and call this inside its onclicklistener

new Comida2().pickEntidad(); //calling function pickEntidad from Comida2 class

Try this. change your pickEntidad like this

private void pickEntidad(){
        final int random = new Random().nextInt(101);

        List<Entidad2> newlistItems = new ArrayList<>();

        Entidad2 entidad1 = new Entidad2(R.drawable.calabacines_3, "Solomillo a la plancha", " 10 min.", 4, 20);
        Entidad2 entidad2 = new Entidad2(R.drawable.patatas_deluxe_especiadas_70523_300_150, "Entrecot", " 15 min.", 2, 50);
        Entidad2 entidad3 = new Entidad2(R.drawable.tomate, "Hamburguesa", " 2 min.", 5, 100);

        int priority1 = entidad1.getPriority();
        int priority2 = entidad2.getPriority();
        int priority3 = entidad3.getPriority();


        if(random < priority1){

            newlistItems.add(entidad1);

        }else if(random < priority2){

            newlistItems.add(entidad2);

        }else if (random <= priority3){

            newlistItems.add(entidad3);

        }

        adaptor.notifyDataSetChanged(); //this another activity listview adaptor so it useless to notify it when you are not this activity
    }
L2_Paver
  • 596
  • 5
  • 11
  • I think this will work, but when I'm running it on the emulator, I get an error message when clicking the cambiarmenu button, and is this: ```java.lang.NullPointerException: Attempt to invoke virtual method 'int com.test.platos_4.Entidad2.getPriority()' on a null object reference at com.test.platos_4.Comida2.pickEntidad(Comida2.java:113) at com.test.platos_4.Menu$3.onClick(Menu.java:44)``` – cristiano ronaldo Oct 03 '19 at 16:32
  • Please help me I think this will work, I need your help – cristiano ronaldo Oct 03 '19 at 16:34
  • try posting your another activity above. – L2_Paver Oct 04 '19 at 00:14
  • entidad1.getPriority(); will produce error because `entidad1` is declare onCreate of comida2 activity. what we do we just call the its function pickEntidad() but we never call the onCreate. solving you current problem will create another problem because of your logic flow. listItems and adaptor is not also declare on pickEntidad so it will create null pointer exception. – L2_Paver Oct 04 '19 at 00:37
  • What I recommend you create your own class for all your function that can be call out by another activities and return your needed parameters/data. – L2_Paver Oct 04 '19 at 00:39