I am creating a todo list kind of app. This is an app with 7 screens to set target and complete that. A screenshot here . Now while coding I realized there is lots of redundancy in the code, like:
1> For 7 day of week i need to create 7 fragments then for all the 7 fragments i need to perform exactly same operations. Can I do this w/o 7 fragments?
2> There are 15 checkboxes and 15 textview now for all-i need to get a reference, then perform onclick operations separately on each one and settext and gettext on each as user try to modify them.
here is one of the textview modify code:
ptext2 = (TextView) view.findViewById(R.id.p_textview2);
ptext2.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setTitle("Target");
alertDialog.setMessage("Set your target");
final EditText input = new EditText(getActivity());
alertDialog.setView(input);
alertDialog.setPositiveButton("Set", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String targetInp = input.getText().toString();
ptext2.setText(targetInp);
p2 = ptext2.getText().toString();
addEventsToDB();
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
alertDialog.show();
return true;
}
});
similarly there are 15 more for textview and then checkbox an I need to do this for all the 7 fragments. Here is my full code of one of the fragment
Since this kind of apps are the first app for most of the users learning android development what should be done to improve this code?
EDIT: For Multiple buttons onclick listener's I am creating an inner class and then using switch for button specific operations.
switch(view.getId()){
case R.id.id1:
//do something
break;
.....
}
From this SO answer. Now I only need to figure out what to do about 7 fragments (Can it be done with just one fragment?)