0

I am trying to retrieve a Cursor from a ORMLite PreparedQuery. My goal is to only paginate my results query, so the device memory won't be too overloaded. I've read Gray's guides on how to use the QueryBuilder object Android Cursor with ORMLite to use in CursorAdapter. Also this one ORMLite with CursorAdapter in Android. But when I try to do: "iterator.getRawResults();" well, the iterator class don't recognize the getRawResults method. It just supports the common methods close(), getEquals(), next(), and so on...

I can retrieve e list of objects successfully from my query, so it isn't a empty query problem...

am I missing something ? My pom.xml is attached.

Here is the test with a list of Objects

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;

}

My failed attempt with Cursors...

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

    }

Here is the project 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>

**EDIT Well, first i was using version 4.9 of ORMLite with isn't really official, and didn't have the methods I wanted to use. After I changed my pom.xml to use android-4.42 and core-4.42 Maven didn't let me compile the program, giving the Dalvik error (“Conversion to Dalvik format failed with error 1”) despite of my attempts to solve this problem, I couldn't.

I created a new project in a new workspace without Maven and added the libs manually, now everything works fine. It seems to be a Maven problem. I hope it helps someone.**

Community
  • 1
  • 1
Anika
  • 398
  • 5
  • 23

1 Answers1

2

when I try to do: "iterator.getRawResults();" well, the iterator class don't recognize the getRawResults method.

I'm confused by this. CloseableIterator has supported this method since version 4.30. You may need to upgrade to a more recent version.

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

You say that this is a failed attempt but the code is correct. Please let me know what exceptions you are getting or what problems you are seeing and I can address them.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • It displays a error : The method getRawCursor() is undefined for the type AndroidDatabaseResults. A i'm using version 4.9 of ormlite-android. – Anika Dec 18 '12 at 12:30
  • @Anika, as my answer mentions, you will need to upgrade to at least 4.30 of ORMLite to get these methods. – Gray Dec 18 '12 at 16:18
  • I've changed my dependencies in the pom.xml archive to the version 4.42 of ormlite-android and ormlite-core, my classes are recognizing everything, thanks. But now i got the following error: Conversion to Dalvik format failed with error 1. – Anika Dec 19 '12 at 14:33
  • Not sure about that error @Anika. Seems like an android thing. – Gray Dec 19 '12 at 14:36
  • Thank you for your cooperation Gray :) – Anika Dec 19 '12 at 15:40
  • Well, the only I achieved to make it work was to create a new workspace and then a new project WITHOUT MAVEN and add the libs I wanted to my class path. Now everything works perfect. – Anika Jan 04 '13 at 19:48
  • Huh. I used ORMLite all of the time with Maven. I wonder what the problem is with your config @Anika. – Gray Jan 04 '13 at 19:50