Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the most appropriate place to call the OpenHelperManager.releaseHelper() in a android custom adapter

Where is the most appropriate place to call the OpenHelperManager.releaseHelper(); in a custom adapter.

For additional context, this adapter is being called in a fragment with a ListView.

Follow from the code snippet below below.

public class CustomAdapter extends BaseAdapter {

    private DatabaseHelper mDatabaseHelper;
    private RuntimeExceptionDao<SomeObject, Long> dao;
    private List<SomeObject> mList;
    private Context context;

    public CustomAdapter(Context context) {
        this.context = context;
        mDatabaseHelper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
        dao = mDatabaseHelper.getRuntimeExceptionDao();

        mList = dao.queryForAll();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = layoutInflater.inflate(R.layout.item, parent, false);

        TextView title = (TextView) row.findViewById(R.id.txtTitle);

        SomeObject obj = mList.get(position);

        title.setText(obj.getTitle());

        return row;
    }
}
like image 848
Horace Heaven Avatar asked Dec 06 '25 04:12

Horace Heaven


1 Answers

Well, as the original author of OpenHelperManager, I'd strongly advise you not use it. Ever. You don't need reference counting, and you don't need to close your DB. Use OrmLiteSqliteOpenHelper directly.

My blog post, where I explain how I wrote that thing, and why you don't need it.

http://touchlabblog.tumblr.com/post/24474750219/single-sqlite-connection

like image 131
Kevin Galligan Avatar answered Dec 07 '25 17:12

Kevin Galligan