如何建立和简化Java代码,安卓

0

的问题

我怎么可以简化所有这些代码? 我创造一个搜索的菜单我要检查的价值观时的用户退出'的文字图'显示出立即为'循环观察者'之下。

  • 一种方式是要做到这一数量非常大的IFs. 做你的建议 另一个方法吗?

  • 我用的是纶图书馆对我的应用程序的数据库。

我想用这个代码,用于这一点,但我看到那些住区中心联是非常高的。

  • 一个朋友建议使用情况的数据库,但是我不知道如何编写其代码!

         public void searchHelper() {
         String sOperationValue = spinnerOperation.getText().toString();
         String sTraderValue = spinnerTraderName.getText().toString();
         String sSearchByValue = spinnerSearchBy.getText().toString();
         long startValue = Long.parseLong(etStartDate.getText().toString());
         long endValue = Long.parseLong(etEndDate.getText().toString());
    
         // * * * * *
         if (!sOperationValue.isEmpty() &&
                 !sTraderValue.isEmpty() &&
                 !sSearchByValue.isEmpty() &&
                 startValue >= 14000000 &&
                 endValue <= 15000000) {
    
         }
         // * - * * *
         if (!sOperationValue.isEmpty() &&
                 sTraderValue.isEmpty() &&
                 !sSearchByValue.isEmpty() &&
                 startValue >= 14000000 &&
                 endValue <= 15000000) {
    
         }
         // * - - - -
         if (!sOperationValue.isEmpty() &&
                 sTraderValue.isEmpty() &&
                 sSearchByValue.isEmpty() &&
                 startValue <= 0 &&
                 endValue <= 0) {
    
         }
         // - * * * *
         if (sOperationValue.isEmpty() &&
                 !sTraderValue.isEmpty() &&
                 !sSearchByValue.isEmpty() &&
                 startValue >= 14000000 &&
                 endValue <= 15000000) {
    
         }
         // Here 'Search By' specifies whether the search should be based on the date of registration or on the date of the transaction.
         // Therefore, when Search By is empty, then the start date and end date values are also empty.
         // - * - - -
         if (sOperationValue.isEmpty() &&
                 !sTraderValue.isEmpty() &&
                 sSearchByValue.isEmpty() &&
                 startValue <= 0 &&
                 endValue <= 0) {
    
         }
         // - - * * *
         if (sOperationValue.isEmpty() &&
                 sTraderValue.isEmpty() &&
                 !sSearchByValue.isEmpty() &&
                 startValue >= 14000000 &&
                 endValue <= 15000000) {
    
         }
         // - - - - -
         if (sOperationValue.isEmpty() &&
                 sTraderValue.isEmpty() &&
                 sSearchByValue.isEmpty() &&
                 startValue <= 0 &&
                 endValue <= 0) {
    
         }
     }
    
  • 我还想写查询使用的情况下却失败。 这是码我写的

     @Query("SELECT * FROM tbl_transaction" +
         " WHERE CASE WHEN operation='null'" +
         " THEN CASE WHEN traderName='null'" +
         " THEN CASE WHEN transactionType ='null'" +
         " THEN CASE WHEN startDate=14000000" +
         " THEN CASE WHEN endDate=15000000" )
         List<Transaction> getSearchValues(String operation, String traderName, String transactionType, long startDate, long endDate);
    

虽然我很多寻找正确的解决方案,不幸的是,我找不到它。

谢谢你提前对你有所帮助。

Form Image

android android-room database java
2021-11-23 22:09:06
2
0

OperationType,TraderName,SearchBy所有的看起来像纺纱。 你在哪里得到的数据填充的选择。 如果其硬编码进入的应用程序取决于你如何实现其不可能的,他们将永远空因为总有一种默认选择的选项。 你可能不需要检查如果他们是空的。 然后你将主要侧重于开始日期和结束日期Edittexts. 然后,您可以进一步改善通过使用日期采摘者,而不是。

2021-11-24 00:13:17
0

我缩短为你,这不是最好的方式做到这一点,但它仍然有效

我将作一个较短的版本,但这将需要一些时间,因为它将更加复杂:

public void searchHelper() {
    boolean b1 = spinnerOperation.getText().toString()isEmpty();
    boolean b2 = spinnerTraderName.getText().toString()isEmpty();
    boolean b3 = spinnerSearchBy.getText().toString()isEmpty();
    boolean b4 = Long.parseLong(etStartDate.getText().toString()) >= 14000000 && Long.parseLong(etEndDate.getText().toString()) <= 15000000;
     boolean b5 = Long.parseLong(etStartDate.getText().toString()) <= 0 && Long.parseLong(etEndDate.getText().toString()) <= 0;

     // * * * * *
     if (!b1 && !b2 && !b3 && b4) {

     }
     // * - * * *
     if (!b1 && b2 && !b3 && b4) {

     }
     // * - - - -
     if (!b1 && b2 && b3 && b5) {

     }
     // - * * * *
     if (b1 && !b2 && !b3 && b4) {

     }
     // Here 'Search By' specifies whether the search should be based on the date of registration or on the date of the transaction.
     // Therefore, when Search By is empty, then the start date and end date values are also empty.
     // - * - - -
     if (b1 && !b2 && b3 && b5) {

     }
     // - - * * *
     if (b1 && b2 && !b3 && b4) {

     }
  // - - - - -
    if (b1 && b2 && b3 && b5) {

  }
 }
2021-11-24 00:02:54

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................