使用基于光标位置的函数调用 - Android

时间:2016-09-15 14:57:16

标签: java android sqlite android-cursor

我目前正在根据查询和根据游标的当前位置修改变量来获取每列。我想知道是否可以通过执行类似这样的操作来减少代码的大小,其中将基于当前被引用的游标中的列来进行不同的函数调用:

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中没有任何东西可以像这样工作,但是我试图减少这里的行数,因为我最终将有近100列将被拉入光标。

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 个答案:

答案 0 :(得分: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来简化这些事情,而不是处理游标。