Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORM Lite with ListView and Mysql backend (android)

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

like image 880
LimitX Avatar asked Feb 01 '26 06:02

LimitX


1 Answers

  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.

like image 140
Barmaley Avatar answered Feb 02 '26 21:02

Barmaley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!