Come costruire e semplificare il codice Java per Android

0

Domanda

Come posso semplificare tutto questo codice? Sto creando un menu di ricerca e ho voglia di controllare i valori quando l'utente esce un 'Testo' e visualizzare l'output immediatamente come Riciclare Viewer' di seguito.

  • Un modo è quello di farlo con un numero molto elevato di IFs. Ti suggeriscono un altro metodo?

  • Io uso la Sala Biblioteca per il mio database.

Ho voluto usare questo codice per questo, ma ho visto che il numero di IFs è molto alta.

  • Un amico ha suggerito di utilizzare Caso nel database, ma non so come scrivere il codice!

         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) {
    
         }
     }
    
  • Volevo anche scrivere una query che utilizza Caso, ma non è riuscito. Questo è il codice che ho scritto

     @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);
    

Anche se ero molto cercando la giusta soluzione, purtroppo, non ho potuto trovare.

Vi ringrazio in anticipo per il vostro aiuto.

Form Image

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

OperationType, TraderName, SearchBy sembrare tutte Filatori. Dove ottenere i dati per compilare le opzioni. Se la sua hardcoded nell'app, a seconda di come implementare la sua improbabile che sarà mai vuoto, dal momento che c'è sempre un opzione predefinita selezionata. Potrebbe non essere necessario per controllare se sono vuote. Quindi sarà per lo più a fuoco sui datainizio e datafine Edittexts. Si potrebbe quindi migliorare ulteriormente con l'uso di Data raccoglitori, invece.

2021-11-24 00:13:17
0

Ho accorciato per voi, questo non è il modo migliore per farlo, ma funziona ancora

Farò una versione più breve di esso, ma ci vorrà un po ' di tempo dal momento che sarà più complicato fare:

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

In altre lingue

Questa pagina è in altre lingue

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