2

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?)

Community
  • 1
  • 1
Amit Tripathi
  • 7,003
  • 6
  • 32
  • 58
  • Is there sth in my answer that you cannot understand? It would solve your problem. – damienix Mar 28 '15 at 19:35
  • I propose to create another more specific question about fragments with a piece of code. It's hard to guess what are you talking about and it's becoming a bit messy here. – damienix Mar 29 '15 at 08:40

1 Answers1

2

What about naming your implementation of View.OnLongClickListener class like:

class OnDayClickListener extends View.OnLongClickListener {

    @Override
    public boolean onLongClick(final View view) {
        // here use view instead of ptext2
    }
}

then use

ptext2.setOnLongClickListener(new OnDayClickListener());
// this repeats for every textbox
damienix
  • 6,463
  • 1
  • 23
  • 30
  • Thanks, it will improve my code. Can you advice mre for this for 7 day of week i am creating 7 fragments then for all the 7 fragments i am performing exactly same operations. Can I do this w/o 7 fragments? – Amit Tripathi Mar 28 '15 at 19:41
  • What do you mean by performing exactly the same operations? I'm not Android developer so I can't advice with fragments particulary. But similar trick can be used with Fragment component probably too. – damienix Mar 28 '15 at 19:52
  • ptext2.setText(targetInp); p2 = ptext2.getText().toString(); How to write a generalized implementation of this for each textview? – Amit Tripathi Mar 28 '15 at 20:14
  • onLongClick(final View view) - view is the actual object that has been clicked. For every object (TextView) that you attach Listener of this class, view will be this clicked object - exactly the one that you need. – damienix Mar 28 '15 at 20:36
  • but vies.setText("") and views.getText() is giving error with views. – Amit Tripathi Mar 28 '15 at 21:28
  • If you describe an error maybe I could guess why it occurs. – damienix Mar 28 '15 at 21:45
  • "Error:(381, 25) error: cannot find symbol method setText()" I think setText() and getText() are not the view class methods. TextView class extends view. I don't know how to resolve this – Amit Tripathi Mar 29 '15 at 04:27
  • Not I got it. I can get the textview using switch(view.getId()){case ...} a do button specific operations in cases. Got help from this SO ansewer http://stackoverflow.com/questions/7873480/android-one-onclick-method-for-multiple-buttons – Amit Tripathi Mar 29 '15 at 05:26
  • If you are sure that your View is always TextView (it is in your case) you can cast it like `final TextView textView = (TextView) view;` inside. Then you can call both methods on `textView`. – damienix Mar 29 '15 at 08:37