call a method in an Android activity

2
public void onDateSet(DatePicker view, int year, int month,int day){
        String date = day+"/"+(month+1)+"/"+year;
  Activity1 act1 = new Activity1();
act1.MostrarDatos(date);
    }

As you can see I want to run a method of the first activity called mostrar datos but it does not work my app closes.

Method MostrarDatos

public void MostrarDatos(String Fecha) {
        try {
            Cursor cursor = basededatos.informacionPrincipal(Fecha); //////////////////////<<<<<<<<<<<<<<<<<< ATENCION
            if (cursor == null) {
                Toast.makeText(this, "Se ha presentado un problema al cargar", Toast.LENGTH_LONG).show();
                return;
            }
            if (cursor.getCount() == 0) {
                Toast.makeText(this, "Ninguna Corte", Toast.LENGTH_LONG).show();
                finish();
                return;
            }
            String[] columns = new String[]{
                    basededatos.COLUMN_ID,
                    basededatos.COLUMN_REGISTRO,
                    basededatos.COLUMN_HUERTO,
                    basededatos.COLUMN_UBICACION,
                    basededatos.COLUMN_ESTADO
            };

            int[] boundTo = new int[]{
                    R.id.txtCodigo_Listado,
                    R.id.txtCreacion_Listado,
                    R.id.txtHuerto_Listado,
                    R.id.txtUbicacion_Listado,
                    R.id.txtEstadoListado
            };

            customAdapter = new CustomAdapter(this,R.layout.item_listado,cursor,columns,boundTo);
            listado.setAdapter(customAdapter);

        } catch (Exception ex) {
            Toast.makeText(this, "Se ha producido un error", Toast.LENGTH_LONG).show();
        }
    }
    
asked by DoubleM 21.12.2016 в 00:37
source

2 answers

4

call a method in an Android activity.

If your method is in your Activity you can define it as static and call it from another application in this way (Considering the Activity where it is located is called MainActivity):

MainActivity.MostrarDatos(date);

What I see could close your application is the context, so you could modify your method:

public static void MostrarDatos(Context context, String Fecha) {
    ... 
    ...
     Toast.makeText(context, "Se ha presentado un problema al cargar", Toast.LENGTH_LONG).show();
    ...
     Toast.makeText(context, "Ninguna Corte", Toast.LENGTH_LONG).show();
    ...
    ...

and you would call the method in this way:

MainActivity.MostrarDatos(getApplicationContext(), date);

The problem in your case is that you refer to a list, which should be outside the method in this case, to work correctly:

        listado.setAdapter(customAdapter);
    
answered by 21.12.2016 в 01:09
3

This NO will work. Since within your method you make a setAdapter of a listView element and you manage ids of elements of the layout of the Activity that you are trying to call.

I recommend that you create a class that is responsible for obtaining the data of your base and return them to manage them from different parts of your project (for example other Activitys .

For example, a class that has methods of insertion, selection, modification and elimination depending on your needs.

    
answered by 21.12.2016 в 00:50