3

I need to build an android application which shows a list of database entries in a ListView.

I came across to this thread and tried the following

CloseableIterator<Parlor> iterator = new ParlorAsync().execute().get();
AndroidDatabaseResults adr = (AndroidDatabaseResults) iterator.getRawResults();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.list_view_template,
                adr.getRawCursor(),
                new String[]{""},
                new int[]{R.id.parlor_name});
parlor_list.setAdapter(adapter);

Where parlor is an entity and ParlorAsync is a AsyncTask which handles the connection to the Mysql Database and returns every row in the database:

JdbcConnectionSource source = new JdbcConnectionSource("jdbc:mysql://192.168.1.1:3306/mydatabase");
source.setUsername("user");
source.setPassword("password");
Dao<Parlor, Integer> parlorDao = DaoManager.createDao(source, Parlor.class);
QueryBuilder<Parlor, Integer> queryBuilder = parlorDao.queryBuilder();
queryBuilder.selectColumns("name");
queryBuilder.orderBy("name", true);
return parlorDao.iterator(queryBuilder.prepare());

As you can see I used a JdbcConnectionSource and this is where the problem begins. It now tells me

com.j256.ormlite.jdbc.JdbcDatabaseResults cannot be cast to com.j256.ormlite.android.AndroidDatabaseResults

This is the list_view_template.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".ParlorListView">

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/parlor_name" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>
</RelativeLayout>

These are my gradle dependencies

compile group: 'com.j256.ormlite', name: 'ormlite-core', version: '4.48'
compile group: 'com.j256.ormlite', name: 'ormlite-jdbc', version: '4.48'
compile group: 'com.j256.ormlite', name: 'ormlite-android', version: '4.48'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'

What am I doing wrong?

Best regards

Community
  • 1
  • 1
LimitX
  • 595
  • 3
  • 7
  • 23

1 Answers1

1
  1. Your particular problem is trying to cast DatabaseResults interface to AndroidDatabaseResults hoping that it's workable - result shows that it's not
  2. Your fundamental problem is trying to extract Cursor from Iterator which is also unreliable.

You should get Cursor from ContentProvider which has to be loaded using LoaderManager and your query should be placed where query() method of ContentProvider sits.

Barmaley
  • 16,638
  • 18
  • 73
  • 146
  • How am I supposed to do that? I got a external mysql database (not local) and I managed to read/write using JdbcConnectionSource, but I still need to get a Cursor from this JdbcConnectionSource ... – LimitX Aug 17 '16 at 11:31
  • You should wrap your jdbc queries with `CursorProvider` - that's it – Barmaley Aug 17 '16 at 13:40