dbhelper,ORMLite和fragment问题 [英] dbhelper , ORMLite and fragments issues

查看:373
本文介绍了dbhelper,ORMLite和fragment问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库有问题。应用程序发送用户写的链接,并作为响应获得链接的简短形式(工作正常)。在我的数据库中,我需要把两个版本的链接 - 完整,短的一个,这里我们有一个问题。我得到这样的错误:

I've got problem with my database. Application sends link written by user and in response get short form of the link (that works fine). In my database I need to put both versions of the link - full , and short one and here we have a problem. I got error like this :

java.lang.IllegalStateException: Could not construct instance of helper class class pl.bartos.task.DatabaseHelper
            at com.j256.ormlite.android.apptools.OpenHelperManager.constructHelper(OpenHelperManager.java:222)
            at com.j256.ormlite.android.apptools.OpenHelperManager.loadHelper(OpenHelperManager.java:170)
            at com.j256.ormlite.android.apptools.OpenHelperManager.getHelper(OpenHelperManager.java:78)
            at pl.bartos.task.Fragments2.getHelper(Fragments2.java:35)
            at pl.bartos.task.Fragments2.onCreateView(Fragments2.java:56)

这是我的第一个应用程序与ORMLite所以我有点困惑。

It's my first app with ORMLite so I'm bit confused.

这是我的fragments2.class:

Here's my fragments2.class :

public class Fragments2 extends SherlockFragment {


private ListView linkListView;
private Dao<Link, Integer> linkDao;
private List<Link> linkList = new ArrayList();

private DatabaseHelper getHelper() {
    if (databaseHelper == null) {
        databaseHelper = OpenHelperManager.getHelper(getActivity(), DatabaseHelper.class);
    }
    return databaseHelper;
}

private DatabaseHelper databaseHelper = null;


public Fragments2() {
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
        savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment2, container, false);
    linkListView = (ListView) rootView.findViewById(R.id.link_lv);

    try {
        linkDao = getHelper().getLinkDao();
        linkList = linkDao.queryForAll();
        final View rowView = inflater.inflate(R.layout.link_item, linkListView, false);
        linkListView.addHeaderView(rowView);
        linkListView.setAdapter(new LinkAdapter(getActivity(), R.layout.link_item, linkList, linkDao));
        populateNoRecordMsg();


    } catch (SQLException e) {
        e.printStackTrace();
    }

    return rootView;
}
private void populateNoRecordMsg()
{
    // If, no record found in the database, appropriate message needs to be displayed.
    if(linkList.size() == 0)
    {
        final TextView tv = new TextView(getActivity());
        tv.setPadding(5, 5, 5, 5);
        tv.setTextSize(15);
        tv.setText("No Record Found !!");
        linkListView.addFooterView(tv);
    }
}


@Override
public void onDetach() {
    super.onDetach();
    try {
        Field childFragmentManager = Fragment.class
                .getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    if (databaseHelper != null) {
        OpenHelperManager.releaseHelper();
        databaseHelper = null;
    }

}

}

这是我的DatabaseHelper类

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

private static final String DATABASE_NAME = "link.db";
private static final int DATABASE_VERSION = 1;
private Dao<Link, Integer> linkDao;

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
}
@Override
public void onCreate(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) {
    try {
        TableUtils.createTable(connectionSource, Link.class);

    } catch (SQLException e) {
        Log.e(DatabaseHelper.class.getName(), "Unable to create database", e);
    }
}
@Override
public void onUpgrade(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource, int oldVer, int newVer) {
    try {

        // In case of change in database of next version of application, please increase the value of DATABASE_VERSION variable, then this method will be invoked
        //automatically. Developer needs to handle the upgrade logic here, i.e. create a new table or a new column to an existing table, take the backups of the
        // existing database etc.

        TableUtils.dropTable(connectionSource, Link.class, true);
        onCreate(sqliteDatabase, connectionSource);

    } catch (SQLException e) {
        Log.e(DatabaseHelper.class.getName(), "Unable to upgrade database from version " + oldVer + " to new "
                + newVer, e);
    }
}
public Dao<Link, Integer> getLinkDao() throws SQLException {
    if (linkDao == null) {
        linkDao = getDao(Link.class);
    }
    return linkDao;
}

}

这是我的适配器

public class LinkAdapter extends ArrayAdapter<String> {

private LayoutInflater inflater;
private List records;
private Dao<Link, Integer> linkDao;
private Context context;


public LinkAdapter(Context context, int resource, List objects, Dao<Link, Integer> linkDao) {
    super(context, resource, objects);

    this.records = objects;
    this.linkDao = linkDao;


}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null)
        convertView = inflater.inflate(R.layout.link_item, parent, false);
    if(records.get(position).getClass().isInstance(new Link())){
        final Link link = (Link) records.get(position);
        ((TextView)convertView.findViewById(R.id.entered_tv)).setText(link.getLongLink());
        ((TextView)convertView.findViewById(R.id.short_tv)).setText(link.getShortLink());
    }
    return convertView;
    }
}

请帮助:/

推荐答案

我知道这是很晚了,但我只是面对这个问题,错误修复我工作了。

I know it's pretty late but I've just faced this problem, and after an hour of 'try and fail' bug-fixing I worked it out.

重新制作ormlite-config.txt修复问题。你应该在你的项目中有类似的类。

Re-making ormlite-config.txt fixed problem. You should have similar class in your project for that task.

示例:

public class DatabaseConfigUtil extends OrmLiteConfigUtil {

private static final Class<?>[] classes = new Class[] {
        Notification.class, Message.class, LocationWrapper.class
};

public static void main(String[] args) throws SQLException, IOException {
    // Provide the name of .txt file which you have already created and kept in res/raw directory
    writeConfigFile("ormlite_config.txt", classes);
}

}

这篇关于dbhelper,ORMLite和fragment问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆