2017-09-02 19 views
0

我有以下助手类,我保留在我的适配器旁边的文件夹中。问题是没有不必要的重复。我需要任一适配器的上下文,具体取决于助手从何处调用,即来自哪个适配器。我如何使正在传递给助手的构造函数的适配器是通用的?如何重构需要来自两个不同适配器的上下文的助手类?

电话:

在呼叫,我设置任何适配器取决于我在哪里为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); 

理想助手:

只是一个建设者:

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

希望这有助于。

或者,您可以在构造对象时将调用方的名称设置为必填字段。这样,你的班级将永远知道是谁创建的,就像分配一个ID一样。

相关问题