2016-09-15 43 views
0

我目前正在根據查詢和每個列基於當前位置的遊標修改變量。我想知道是否有可能做這樣的事情,其中​​一個不同的函數調用將基於當前正在引用的遊標中列進行,以減少代碼的大小:基於光標位置使用函數調用 - Android

do { 
    Ticket ticket = new Ticket(); 
    for(int i = 0; i < cursor.getColumnCount(); i++) 
    { 
     if (cursor.getString(0) != null) { 
     /*Where the array contains a list of function calls*/ 
     ticket.arrayList(i); 
    } 
}while(cursor.moveToNext()); 

以下是我目前擁有的代碼。從我所知道的Java中沒有任何工作是這樣的,但我試圖減少這裏的行數,因爲我最終會有將近一百個列將被拉入光標。

public List<Ticket> getTickets(Context context, SQLiteDatabase db) 
{ 
    List<Ticket> ticketInfo = new ArrayList<>(); 
    String selectQuery = "SELECT * FROM " + TABLE_TICKET; 

    Cursor cursor = null; 


    try { 
     cursor = db.rawQuery(selectQuery, null); 
     if (cursor != null) { 
      try { 
       if (cursor.moveToFirst()) { 
        do { 
         Ticket ticket = new Ticket(); 
         //Set the ticket number 
         if (cursor.getString(0) != null) { 
          ticket.setTicketNr(Integer.parseInt(cursor.getString(0))); 
         } 
         //Set the ticket id 
         if (cursor.getString(1) != null) { 
          ticket.setTicketId(Integer.parseInt(cursor.getString(1))); 
         } 
         // 
         if (cursor.getString(2) != null) { 
          ticket.setServiceName(cursor.getString(2)); 
         } 
         // 
         if (cursor.getString(3) != null) { 
          ticket.setServiceHouseNr(Integer.parseInt(cursor.getString(3))); 
         } 
         // 
         if (cursor.getString(4) != null) { 
          ticket.setServiceDirectional(cursor.getString(4)); 
         } 
         // 
         if (cursor.getString(5) != null) { 
          ticket.setServiceStreetName(cursor.getString(5)); 
         } 
         // 
         if (cursor.getString(6) != null) { 
          ticket.setServiceCommunityName(cursor.getString(6)); 
         } 
         // 
         if (cursor.getString(7) != null) { 
          ticket.setServiceState(cursor.getString(7)); 
         } 
         // 
         if (cursor.getString(8) != null) { 
          ticket.setServiceZip1(Integer.parseInt(cursor.getString(8))); 
         } 
         // 
         if (cursor.getString(9) != null) { 
          ticket.setServiceZip2(Integer.parseInt(cursor.getString(9))); 
         } 
         // 
         if (cursor.getString(10) != null) { 
          ticket.setTroubleReported(cursor.getString(10)); 
         } 
         // Adding exercise to list 
         if (ticket != null) { 
          ticketInfo.add(ticket); 
         } 

        } while (cursor.moveToNext()); 
       } else { 
        //No results from query 
        Toast.makeText(context.getApplicationContext(), "No tickets found", Toast.LENGTH_LONG).show(); 
       } 

      } finally { 
       if (cursor != null && !cursor.isClosed()) { 
        cursor.close(); 
       } 
      } 
     } 
    } 
    catch(SQLiteException exception)//If exception is found 
    { 
     Log.d(TAG, "Error", exception); 
     //Display exception 
     Toast.makeText(context.getApplicationContext(), exception.toString(), Toast.LENGTH_LONG).show(); 
    } 

    return ticketInfo; 
} 

謝謝你對此的任何見解。

回答

1

我認爲這樣做。只需推進光標並將其傳遞給Ticket構造函數即可。您可能想要添加一些錯誤檢查。

public class Ticket { 

    private static class Field { 
     int intValue; 
     String stringValue; 
     final Class type; 

     Field(Class fieldType){ 
      type = fieldType; 
     } 

     void set(String value){ 
      if(type.equals(String.class)){ 
       stringValue = value; 
      } 
      else { 
       intValue = Integer.parseInt(value); 
      } 
     } 
    } 

    private List<Field> fields = new ArrayList<>(); 

    private Field addField(Field field){ 
     fields.add(field); 
     return field; 
    } 

    // This solution relies on adding fields in the order they'll be retrieved in the cursor. 
    // Other options are possible such as a map by column index. 
    private Field ticketNumber = addField(new Field(Integer.class)); 
    private Field serviceName = addField(new Field(String.class)); 

    public Ticket(Cursor cursor){ 
     for(int i=0; i < fields.size(); i++){ 
      Field f = fields.get(i); 
      f.set(cursor.getString(i)); 
     } 
    } 
} 

public int getTicketNumber(){ 
    return ticketNumber.intValue; 
} 

// Don't know if you need setters 
public void setTicketNumber(int value){ 
    ticketNumber.intValue = value; 
} 

// etc for remaining fields 

我也會考慮使用ORM來使這個東西更容易,而不是處理遊標。

+0

非常感謝!在我的DatabaseHandler類中進行了一些修改後,我能夠讓您的解決方案發揮作用。 –