0

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

我可以从我的查询中成功检索对象列表,所以这不是一个空查询问题......

我错过了什么吗?我的 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 错误(“Conversion to Dalvik format failed with error 1”) ,我做不到。

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

4

1 回答 1

2

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

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

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

您说这是一次失败的尝试,但代码是正确的。请让我知道您遇到什么异常或遇到什么问题,我可以解决它们。

于 2012-12-17T18:16:00.917 回答