Doubt about android using classes

0

I have the following method implemented and I will use it in several Activities

private int dia, mes, ano;
String data;
String dd, mm;

int hora, minuto;
static final int TIME_DIALOG_ID = 0;
String horas;

@Override
public void onCreate(Bundle savedInstaceState){
    super.onCreate(savedInstaceState);
    setContentView(R.layout.sub_casa_quarto);

    Calendar c = Calendar.getInstance();
    ano = c.get(Calendar.YEAR);
    mes = c.get(Calendar.MONTH);
    dia = c.get(Calendar.DAY_OF_MONTH);

    hora=c.get(Calendar.HOUR);
    minuto=c.get(Calendar.MINUTE);

    if (mes >=1 && mes<10){
        mm = "0"+String.valueOf(mes);
    }
    else
        mm = String.valueOf(mes);

    if (dia>= 1 && dia<10){
        dd = "0"+String.valueOf(dia);
    }
    else
        dd = String.valueOf(dia);

    data = String.valueOf(ano) + "-" + mm + "-"+dd;
}

//Data
public void onCLickteste(View v){
    final DatePickerDialog dialogo = new DatePickerDialog(this, mDataSetListener, ano, mes, dia);
    dialogo.show();
}

private OnDateSetListener mDataSetListener = new OnDateSetListener(){

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth){
        int a = year;
        int m = monthOfYear;
        int d = dayOfMonth;
        if (m >= 1 && m<10){
            mm = "0"+String.valueOf(m);
        }else
            mm = String.valueOf(m);
        if (d>=1 && d<10){
            dd = "0" + String.valueOf(d);
        }else
            dd = String.valueOf(d);

        data = String.valueOf(ano) + "-" +mm+ "-" +dd;
        mostrarData();
    }
};

private void mostrarData(){
    Toast.makeText(this, "Data Selecionada" + data,Toast.LENGTH_LONG).show();
}

I would like to create a class and put it there so I do not need to repeat the code. Could someone help me or explain how to do this? I need the return (result) to save to the database.

    
asked by anonymous 30.03.2014 / 16:36

2 answers

1

If you use this method only in Activities you can create a GenericActivity extends Activity and extend your classes from it, so the method you create ( public or protected ) can always be accessed by your daughters. Ex.:

public abstract class GenericActivity extends Activity {
.
.
.
/*Mostra uma mensagem de alerta*/
protected void showNeutralDialog(Context context, String title, String message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    if (title != null) {
        builder.setTitle(title);
    }
    builder.setMessage(message);

    builder.setNeutralButton("OK", null);
    builder.create().show();
}

And in the daughters you'll use it like this:

public class MinhaTela extends GenericActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.telaTeste);
    showNeutralDialog(MinhaTela.this, "Título", "Isso é um teste.");

 }
}

In the example it shows a neutral alert message whenever we call the method and this call can be made from any class that extends from GenericActivity . Try to apply this concept in your classes and generalize your method.

    
02.04.2014 / 03:11
0

You can create a class that will have static methods that will perform these operations. These methods must be given all the dependencies required to perform the operation and return the type of data that you expect.

Example:

public class Util {
    public static Integer executa (int arg1, int arg3, ... , int argN)  {
       .
       .
       .

       return value;
    }    
}
    
31.03.2014 / 00:26