ยังไงเพื่อสร้างและ simplify รหัสของจาวาสำหรับ Android

0

คำถาม

ยังไงฉัน simplify ทั้งหมดนี่รหัสหรอ? ฉันกำลังสร้างเป็นการค้นหาของเมนูและฉันต้องตรวจสอบค่าต่างๆที่ผู้ใช้ออกจากเป็นข้อความในมุมมองและมีการแสดงผลส่งออกทันทีที่เป็น'Recycle เครื่องมือแสดง'ทางด้านล่างนี้

  • ทางเดียวคือต้องทำอย่างนี้กับตัวใหญ่กว่าจำนวนของคำว่าถ้า คุณจะแนะนำว่า วิธีอื่นอีกมั้ย

  • ฉันใช้ Roon องสมุดของโปรแกรมฐานข้อมูล

ฉันอยากจะใช้รหัสสำหรับเรื่องนี้เลยแต่ฉันเห็นว่าจำนวนของเงื่อนไขคือสูงมาก.

  • เพื่อนแนะนำให้ใช้ในกรณีที่ลอกฐานข้อมูลแต่ฉันไม่รู้ว่าต้องเขียนมันรหัส!

         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 นดูเหมือน Spinners. บอกได้แค่ว่าผมมีแหล่งข้อมูลออกไป populate ทางเลือก ถ้ามัน hardcoded เข้าไปในแอ๊ปขึ้นอยู่กับว่าคุณด้วยอะไรซักอย่างมันไม่น่าจะเป็นพวกเขาต้องเคยว่างเปล่าตั้งแต่นั้นคือเสมออกตัวเลือกค่าปริยายที่เลือกไว้ คุณอาจจะไม่ต้องตรวจสอบว่าพวกเขาว่างเปล่า งั้นคุณก็ส่วนใหญ่จะเป็นการจดจ่ออยู่กับค startDate และ endDate Edittexts. คุณอาจจะนั้นปรับปรุงต่อโดยการใช้วัน pickers แทน

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ý
..................................................................................................................