Search code examples
javaandroidsqliteandroid-cursor

Using Function Call Based on Cursor Position - Android


I am currently taking each column based on query and modifying variables based on the current position of the cursor. I was wondering if it would be possible to cut down the size of the code by doing something like this where a different function call would be made based on the column within the cursor that is currently being referenced:

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

Below is the code I currently have. From what I know there isn't anything in Java that works like this, but I'm trying to cut down on the number of lines here as I will eventually have close to one hundred columns that will be pulled into the cursor.

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;
}

Thank you for any insights into this.


Solution

  • I think this would do it. Just advance the cursor and pass it into the Ticket constructor. You may want to add some error checking.

    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
    

    I would also consider using an ORM to make this stuff easier, rather than dealing with cursors.