Doubt about android using classes

Asked

Viewed 443 times

0

I have the following method implemented and I will use it in various 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 don’t have to repeat the code. Could someone help me or explain how to do this? I need the return (result) to save to the database.

  • You can use Fragments to do this. See this post

2 answers

1


If you use this method only in Activities you can create a GenericActivity extends Activity and extend that your classes of it, thus the method created (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 daughters you will wear so:

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 warning 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.

0

You can create a class that will have static methods that will perform these operations. These methods should receive all the dependencies needed to perform the operation and return the data type you expect.

Example:

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

       return value;
    }    
}

Browser other questions tagged

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