【问题标题】:Using Function Call Based on Cursor Position - Android使用基于光标位置的函数调用 - Android
【发布时间】:2016-09-18 02:16:39
【问题描述】:

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

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

感谢您对此的任何见解。

【问题讨论】:

    标签: java android sqlite android-cursor


    【解决方案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 来简化这些事情,而不是处理游标。

    【讨论】:

    • 非常感谢!通过对我的 DatabaseHandler 类进行一些修改,我能够让您的解决方案发挥作用。
    猜你喜欢
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 2018-07-26
    • 2011-04-08
    • 2015-01-28
    • 1970-01-01
    相关资源
    最近更新 更多