使用ORMLite从DAOObject获取Android Cursor的问题

时间:2012-12-13 14:51:27

标签: android ormlite android-cursor

我正在尝试从ORMLite PreparedQuery中检索一个Cursor。 我的目标是仅对结果查询进行分页,因此设备内存不会过载。 我已经阅读了有关如何使用QueryBuilder对象Android Cursor with ORMLite to use in CursorAdapter的Gray指南。 这一个ORMLite with CursorAdapter in Android。 但是当我尝试这样做时:“iterator.getRawResults();”好吧,迭代器类不识别getRawResults方法。它只支持常用方法close(),getEquals(),next()等等......

我可以从查询中成功检索到对象的e列表,因此它不是一个空查询问题...

我错过了什么吗? 我的pom.xml已附上。

这是带有对象列表的测试

public List<Produto> getProdutoDecricao2(String padrao) throws SQLException {

    List<Produto> listaProduto = null;
    QueryBuilder<Produto, String> queryBuilder = produtoDao.queryBuilder();
    Where<Produto, String> where = queryBuilder.where();
    SelectArg selectArg = new SelectArg();
    selectArg.setValue('%'+padrao+'%');
    where.like("descricao", selectArg);

    PreparedQuery<Produto> preparedQuery = queryBuilder.prepare();      
    listaProduto = produtoDao.query(preparedQuery);

    return listaProduto;

}

我尝试使用游标失败了......

//creation of DAO
private Dao<Produto,String> produtoDao;

public Cursor getProdutoDecricao(String padrao)  {
    List<Produto> listaProduto = null;
    QueryBuilder<Produto, String> queryBuilder = produtoDao.queryBuilder();
    Where<Produto, String> where = queryBuilder.where();
    SelectArg selectArg = new SelectArg();
    selectArg.setValue('%'+padrao+'%');
    where.like("descricao", selectArg);

// when you are done, prepare your query and build an iterator
    PreparedQuery<Produto> preparedQuery =  queryBuilder.prepare();
    CloseableIterator<Produto> iterator = produtoDao.iterator(preparedQuery);

    AndroidDatabaseResults results = (AndroidDatabaseResults)iterator.getRawResults();
    Cursor cursor = results.getRawCursor();

    iterator.close();


    return cursor;

    }

这是项目pom.xml

<properties>
    <android-platform>8</android-platform>
    <android-maven-plugin-version>3.4.0</android-maven-plugin-version>
    <maven-compiler-plugin-version>2.3.2</maven-compiler-plugin-version>
    <java-version>1.6</java-version>
    <maven-eclipse-plugin-version>2.8</maven-eclipse-plugin-version>
    <org.codehaus.jackson-version>1.9.7</org.codehaus.jackson-version>
</properties>

<repositories>         
    <repository>
        <id>org.springframework.maven.snapshot</id>
        <name>Spring Maven Snapshot Repository</name>
        <url>http://maven.springframework.org/snapshot</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>         <!-- For developing against latest Spring milestones -->
    <repository>
        <id>org.springframework.maven.milestone</id>
        <name>Spring Maven Milestone Repository</name>
        <url>http://maven.springframework.org/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>

</repositories>
<build>
    <finalName>${project.artifactId}</finalName>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <version>${android-maven-plugin-version}</version>
            <configuration>
                <sdk>
                    <platform>${android-platform}</platform>
                    <path>/Users/rav/Documents/Android/android-sdk-macosx</path>
                </sdk>
                <emulator>
                    <avd>Android4.1</avd>
                </emulator>
                <deleteConflictingFiles>true</deleteConflictingFiles>
                <undeployBeforeDeploy>true</undeployBeforeDeploy>
            </configuration>
            <extensions>true</extensions>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin-version}</version>
            <configuration>
                <source>${java-version}</source>
                <target>${java-version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>${maven-eclipse-plugin-version}</version>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <!--This plugin's configuration is used to store Eclipse m2e settings 
                only. It has no influence on the Maven build itself. -->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                                    <artifactId>android-maven-plugin</artifactId>
                                    <versionRange>[3.1.1,)</versionRange>
                                    <goals>
                                        <goal>proguard</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
<dependencies>
    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android</artifactId>
        <version>2.2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.android</groupId>
        <artifactId>spring-android-rest-template</artifactId>
        <version>1.0.0.RELEASE</version>
    </dependency> 
    <dependency>
        <groupId>com.j256.ormlite</groupId>
        <artifactId>ormlite-android</artifactId>
        <version>4.9</version>
    </dependency>
     <dependency>
            <groupId>com.j256.ormlite</groupId>
            <artifactId>ormlite-android</artifactId>
            <version>4.9</version>
            <exclusions>
                <exclusion>
                    <groupId>com.j256.ormlite</groupId>
                    <artifactId>ormlite-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    <dependency>
        <groupId>org.roboguice</groupId>
        <artifactId>roboguice</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.findbugs</groupId>
        <artifactId>jsr305</artifactId>
        <version>1.3.9</version>
     </dependency>
    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>support-v4</artifactId>
        <version>r6</version>
    </dependency>
    <dependency>
        <groupId>jgilfelt.android-viewbadger</groupId>
        <artifactId>android-viewbadger</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <!-- Using Jackson for JSON marshaling -->
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${org.codehaus.jackson-version}</version>
    </dependency>
</dependencies>

**编辑 好吧,首先我使用的是ORMLite版本4.9并不是真正的官方,并且没有我想要使用的方法。在我改变我的pom.xml以使用android-4.42和core-4.42后,Maven没有让我编译程序,给出了Dalvik错误(“转换为Dalvik格式失败,错误1”)尽管我试图解决这个问题,我不能。

我在没有Maven的新工作区中创建了一个新项目并手动添加了库,现在一切正常。这似乎是一个Maven问题。 我希望它可以帮助别人。**

1 个答案:

答案 0 :(得分:2)

  

当我尝试做:“iterator.getRawResults();”好吧,迭代器类不识别getRawResults方法。

我对此感到困惑。从版本4.30开始,CloseableIterator已支持此方法。您可能需要升级到更新版本。

AndroidDatabaseResults results =
    (AndroidDatabaseResults)iterator.getRawResults();
Cursor cursor = results.getRawCursor();

你说这是尝试失败但代码是正确的。请告诉我您遇到的例外或您遇到的问题,我可以解决这些问题。