John John - 13 days ago 5
Android Question

How to reuse code

So this is my reminder app screen shot, as you can see it has a + button and also a button. When the + button is cliked, a dialog with editText will be displayed.

enter image description here

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_task:
mClickedPosition = -1;
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = LayoutInflater.from(getActivity());
View promptView = getActivity().getLayoutInflater().inflate(R.layout.dialog_with_edittext, null);
Button cancel = (Button) promptView.findViewById(R.id.cancelBtn);
Button save = (Button) promptView.findViewById(R.id.okBtn);
task = (EditText) promptView.findViewById(R.id.task);
time = (EditText) promptView.findViewById(R.id.time);
date = (EditText) promptView.findViewById(R.id.date);
final AlertDialog alert = builder.create();
date.setOnClickListener(this);

time.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Fragment fragDialog = getActivity().getSupportFragmentManager().findFragmentByTag("TimePicker");
if (fragDialog == null) { // Fragment not added
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
tp.show(ft, "TimePicker");
} else {
// already active
}
}
});

cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
alert.dismiss();
}
});

save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String addTask = task.getText().toString();
String time1 = time.getText().toString();
String date1 = date.getText().toString();
if ((time1.trim().equals(""))|| (date1.trim().equals("") )|| (addTask.trim().equals(""))) {
Toast.makeText(getActivity(), " Not Completed", Toast.LENGTH_SHORT).show();
}
else {
if (adapter != null) {
if (mClickedPosition == -1) {
adapter.add(iD, addTask, time1, date1);
insertTask(addTask, time1, date1);
listview.setAdapter(adapter);
alert.dismiss();
check();
retrieveList(name);
}
}

Calendar alarm = Calendar.getInstance();
Calendar copy = (Calendar) alarm.clone();
alarm.set(Calendar.YEAR, year1);
alarm.set(Calendar.MONTH, month1);
alarm.set(Calendar.DAY_OF_MONTH, day1);
alarm.set(Calendar.HOUR_OF_DAY, hour1);
alarm.set(Calendar.MINUTE, min1);
int result = alarm.compareTo(copy);

if(result < 0) {
alarm.add(Calendar.DATE, 1);
}
else
{
Intent myIntent = new Intent(getActivity(), MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, alarm.getTimeInMillis(), pendingIntent);
}
}
}
});

alert.setView(promptView);
alert.show();
return true;


If the button is clicked, it will open dialog exactly same as the + button. My question now is, is there a way I can reuse the code so the code will look a bit shorter, or I have to copy the code and paste to button
onClick
?

Show you my full code of the activity. As you can see, most of the code are the same (+button, button and also the listView click)

http://pastebin.com/HEKcmVK3

Answer

You can create a method of this part of code and then just call the method inside the onclicklistener

String addTask = task.getText().toString();
String time1 = time.getText().toString();
String date1 = date.getText().toString();
if ((time1.trim().equals(""))|| (date1.trim().equals("") )|| (addTask.trim().equals(""))) {
    Toast.makeText(getActivity(), " Not Completed", Toast.LENGTH_SHORT).show();
}
else {
    if (adapter != null) {
        if (mClickedPosition == -1) {
            adapter.add(iD, addTask, time1, date1);
            insertTask(addTask, time1, date1);
            listview.setAdapter(adapter);
            alert.dismiss();
            check();
            retrieveList(name);
        }
    }

    Calendar alarm = Calendar.getInstance();
    Calendar copy = (Calendar) alarm.clone();
    alarm.set(Calendar.YEAR, year1);
    alarm.set(Calendar.MONTH, month1);
    alarm.set(Calendar.DAY_OF_MONTH, day1);
    alarm.set(Calendar.HOUR_OF_DAY, hour1);
    alarm.set(Calendar.MINUTE, min1);
    int result = alarm.compareTo(copy);

    if(result < 0) {
        alarm.add(Calendar.DATE, 1);
    }
    else
    {
        Intent myIntent = new Intent(getActivity(), MyReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
        AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC, alarm.getTimeInMillis(), pendingIntent);
    }
}