2017-09-02 4 views
0

私は自分のアダプタの横にあるフォルダに次のヘルパークラスを持っています。問題は不要な重複があることです。ヘルパーがどこから呼び出されたか、つまりどのアダプタから呼び出されたかに応じて、いずれかのアダプタのコンテキストが必要です。どのようにヘルパーのコンストラクタに渡されているアダプタを一般的なものにすることができますか?2つの異なるアダプタのコンテキストを必要とするHelperクラスをどのようにリファクタリングできますか?

コール:コールで

、私は私が場所に応じてnullにアダプタのいずれか一方を設定します。

private final QuickReplyDialogHelper quickReplyDialogHelper = new QuickReplyDialogHelper(this, null); 
quickReplyDialogHelper.quickReplyDialog(object); 

ヘルパー:

class QuickReplyDialogHelper { 

    private final UserProfileAdapter userProfileAdapter; 
    private final FeedAdapter feedAdapter; 

    QuickReplyDialogHelper(UserProfileAdapter userProfileAdapter, FeedAdapter feedAdapter) { 
     this.userProfileAdapter = userProfileAdapter; 
     this.feedAdapter = feedAdapter; 
    } 

    void quickReplyDialog(ParseObject object) { 

     if (userProfileAdapter != null) { 
      CharSequence colors[] = new CharSequence[]{ 
        "1", 
        "2", 
        "3", 
      }; 

      AlertDialog.Builder builder = new AlertDialog.Builder(userProfileAdapter.getmContext()); 
      builder.setTitle("Quick Reply"); 
      builder.setIcon(R.drawable.ic_quick_reply); 
      AlertDialog.Builder builder1 = builder.setItems(colors, (dialog, which) -> { 
       String quickReply = null; 
       if (which == 0) { 
        quickReply = "1"; 
        Toast.makeText(userProfileAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show(); 
       } else if (which == 1) { 
        quickReply = "2"; 
        Toast.makeText(userProfileAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show(); 
       } else if (which == 2) { 
        quickReply = "3"; 
        Toast.makeText(userProfileAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show(); 
       } 

       ParseObject message = new ParseObject("Object"); 
       message.put(ParseConstants.KEY_SENDER_AUTHOR_POINTER, ParseUser.getCurrentUser()); 
       message.put("replyAuthor", object.getParseObject(ParseConstants.KEY_SENDER_AUTHOR_POINTER)); 
       String[] likedBy = new String[0]; 
       message.put(ParseConstants.KEY_LIKED_BY, Arrays.asList(likedBy)); 
       message.put(ParseConstants.KEY_SENDER_PARSE_OBJECT_ID, object.getObjectId()); 
       message.put(ParseConstants.KEY_NOTIFICATION_TEXT, quickReply); 
       message.saveInBackground(); 

      }); 
      builder1.show(); 
     } 

     if (feedAdapter != null) { 
      CharSequence colors[] = new CharSequence[]{ 
        "1", 
        "2", 
        "3" 
      }; 

      AlertDialog.Builder builder = new AlertDialog.Builder(feedAdapter.getmContext()); 
      builder.setTitle("Quick Reep"); 
      builder.setIcon(R.drawable.ic_quick_reply); 
      AlertDialog.Builder builder1 = builder.setItems(colors, (dialog, which) -> { 
       String quickReply = null; 
       if (which == 0) { 
        quickReply = "1"; 
        Toast.makeText(feedAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show(); 
       } else if (which == 1) { 
        quickReply = "2"; 
        Toast.makeText(feedAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show(); 
       } else if (which == 2) { 
        quickReply = "3"; 
        Toast.makeText(feedAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show(); 
       } 

       ParseObject message = new ParseObject("Object"); 
       message.put(ParseConstants.KEY_SENDER_AUTHOR_POINTER, ParseUser.getCurrentUser()); 
       message.put("replyAuthor", object.getParseObject(ParseConstants.KEY_SENDER_AUTHOR_POINTER)); 
       String[] likedBy = new String[0]; 
       message.put(ParseConstants.KEY_LIKED_BY, Arrays.asList(likedBy)); 
       message.put(ParseConstants.KEY_SENDER_PARSE_OBJECT_ID, object.getObjectId()); 
       message.put(ParseConstants.KEY_NOTIFICATION_TEXT, quickReply); 
       message.saveInBackground(); 

      }); 
      builder1.show(); 
     } 


    } 
} 

理想コール:

private final QuickReplyDialogHelper quickReplyDialogHelper = new QuickReplyDialogHelper(this); 
quickReplyDialogHelper.quickReplyDialog(object); 

理想のヘルパー:

ただ1つのビルダー:

CharSequence colors[] = new CharSequence[]{ 
      "1", 
      "2", 
      "3" 
    }; 

    AlertDialog.Builder builder = new AlertDialog.Builder(genericAdapter.getmContext()); 
    builder.setTitle("Quick Reep"); 
    builder.setIcon(R.drawable.ic_quick_reply); 
    AlertDialog.Builder builder1 = builder.setItems(colors, (dialog, which) -> { 
     String quickReply = null; 
     if (which == 0) { 
      quickReply = "1"; 
      Toast.makeText(genericAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show(); 
     } else if (which == 1) { 
      quickReply = "2"; 
      Toast.makeText(genericAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show(); 
     } else if (which == 2) { 
      quickReply = "3"; 
      Toast.makeText(genericAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show(); 
     } 

     ParseObject message = new ParseObject("Object"); 
     message.put(ParseConstants.KEY_SENDER_AUTHOR_POINTER, ParseUser.getCurrentUser()); 
     message.put("replyAuthor", object.getParseObject(ParseConstants.KEY_SENDER_AUTHOR_POINTER)); 
     String[] likedBy = new String[0]; 
     message.put(ParseConstants.KEY_LIKED_BY, Arrays.asList(likedBy)); 
     message.put(ParseConstants.KEY_SENDER_PARSE_OBJECT_ID, object.getObjectId()); 
     message.put(ParseConstants.KEY_NOTIFICATION_TEXT, quickReply); 
     message.saveInBackground(); 


    }); 
    builder1.show(); 

答えて

0

では、コールがどこから来ているかを決定するためにスタックトレースを使用してみましたか?あなたのコードに多くの行が追加されるかもしれませんが、各呼び出しの宛先をちょっと知ることが簡単になります。

このリンクは役立つかもしれない: How to get the caller class in Java

は、この情報がお役に立てば幸いです。

また、オブジェクトを構築するときに、呼び出し元の名前を必須フィールドにすることもできます。そうすれば、あなたのクラスはいつ誰がそれを作成したのかを常に知ることができます。

関連する問題