开发者

ListActivity ormlite problem?

开发者 https://www.devze.com 2023-02-07 20:05 出处:网络
I have a rather large amount of code written when I decided to use ORMLite. After reading the doc I found that I would need to extend like:

I have a rather large amount of code written when I decided to use ORMLite.

After reading the doc I found that I would need to extend like:

MyClass extends OrmLiteBaseActivity<DatabaseHelper>

but I have already extended i开发者_运维问答t with ListActivity.

Is is possible to do it without extending OrmLiteBaseActivity?

Tnx in advance.


It is not a requirement to extend OrmLiteBaseActivity. You'll just need to manage more of the utility functions yourself.

Your best option would be to create your own DatabaseHelper inside your activity and to manage how many users there are of it and to discard it when it is done being used. Generally speaking, this is the utility that the OrmLiteBaseActivity gives to you. A mechanism which will manage your database objects for you. It's just a convenience.


Example:

private static Dao<Agent, Object> agentDao = null;
public void someMethod() {
    if(agentDao == null){
      helper = (MyDBHelper) OpenHelperManager.getHelper(getContext());
      try {
        agentDao = helper.getAgentDao();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }

I had implemented a method to return my DAOs on the class MyDBHelper. Take a look at the ORMLite Android Javadoc as well as the more general ORMLite Core Javadoc. There are lots of good examples out there.


[ @Nick's answer is fine but I thought I'd add more information. ]

ORMLite is missing a OrmLiteBaseListActivity class that was added in version 4.10 -- sorry about the miss. In the meantime, you can easily create your own version of this class by copying the OrmLiteBaseTabActivity class changing the class that it extends from TabActivity to ListActivity. Then change all of your list activity classes to extend this new class. Once 4.10 is out then you can go back and remove the class.

For example:

public abstract class OrmLiteBaseListActivity<H extends OrmLiteSqliteOpenHelper>
    extends ListActivity {
    // insert contents of the OrmLiteBaseTabActivity class here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消