Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7808775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:13:50+00:00 2026-06-02T03:13:50+00:00

I am making an app for cooking. I made three SQL databases and I

  • 0

I am making an app for cooking. I made three SQL databases and I populate a list with a tabview.
This is the database

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Cook_tab_mains_data extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "mains";

public Cook_tab_mains_data(Context context) {
    super(context, DATABASE_NAME, null, 2);
}

@Override
public void onCreate(SQLiteDatabase db) {

    String sql = "CREATE TABLE IF NOT EXISTS mains (" +
                    "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                    "name TEXT, " +
                    "disc TEXT, " +
                    "photo TEXT, " +
                    "prep TEXT, " +
                    "thumb TEXT, " +
                    "ingre TEXT, " +
                    "howto TEXT, " +
                    "info TEXT, " +
                    "mainId INTEGER)";
    db.execSQL(sql);

    ContentValues values = new ContentValues();


    values.put("name", "Name 3");
    values.put("disc", "here is the description");
    values.put("photo", "stub.png");
    values.put("ingre", "the ingredients of the mains");
    values.put("howto", "how to make this thing");
    values.put("info", "basically its this much calorie and such and such");
    db.insert("mains", "name", values);

    values.put("name", "Name 4");
    values.put("disc", "here is the description");
    values.put("photo", "stub.png");
    values.put("thumb", "ic_launcher.png");
    values.put("ingre", "the ingredients of the mains");
    values.put("howto", "how to make this thing");
    values.put("info", "basically its this much calorie and such and such");
    db.insert("mains", "name", values);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS mains");
    onCreate(db);
}}

As you can see my database is static.
Now I have a list activity that lists the items and another that details them:

import java.io.IOException;
import java.io.InputStream;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class Cook_tab_mains extends ListActivity {

protected EditText searchText;
protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;
 class CustomSimpleCursor extends SimpleCursorAdapter {

        public CustomSimpleCursor(Context context, int layout, Cursor c,
                        String[] from, int[] to) {
                super(context, layout, c, from, to);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
                super.bindView(view, context, cursor);
                ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
                try {
                        InputStream bitmap = getAssets()
                                        .open(cursor.getString(cursor
                                                        .getColumnIndexOrThrow("thumb")));
                        Bitmap bit = BitmapFactory.decodeStream(bitmap);
                        thumb.setImageBitmap(bit);
                } catch (IOException e1) {
                        e1.printStackTrace();
                }

        }
}


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cook_tab_general);
    db = (new Cook_tab_mains_data(this)).getWritableDatabase();
    searchText = (EditText) findViewById (R.id.searchText);

EditText searchTo = (EditText)findViewById(R.id.searchText);
searchTo.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable s) {
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // autoclick
    } 
});

    // || Query  SQLite
    cursor = db.rawQuery("SELECT _id, name, disc, thumb, prep FROM mains WHERE name LIKE ?", 
                    new String[]{"%" + searchText.getText().toString() + "%"});


    adapter = new CustomSimpleCursor(
            this, 
            R.layout.cook_tab_generalist, 
            cursor, 
            new String[] {"name", "disc", "prep"}, 
            new int[] {R.id.name, R.id.disc, R.id.prep});

    setListAdapter(adapter);}

public void search(View view) {
    // ||Query SQLite
    cursor = db.rawQuery("SELECT _id, name, disc, thumb, prep  FROM mains WHERE name LIKE ?", 
                    new String[]{"%" + searchText.getText().toString() + "%"});

        adapter = new CustomSimpleCursor(
                this, 
                R.layout.cook_tab_generalist, 
                cursor, 

                new String[] {"name", "disc", "prep"}, 
                new int[] {R.id.name, R.id.disc, R.id.prep});
        setListAdapter(adapter);}


public void onListItemClick(ListView parent, View view, int position, long id) {
    Intent intent = new Intent(this, Cook_tab_mains_details.class);
    Cursor cursor = (Cursor) adapter.getItem(position);
    intent.putExtra("MAINS_ID", cursor.getInt(cursor.getColumnIndex("_id")));
    startActivity(intent);
}
}

and then when an item is selected it shows details in this activity:

import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class Cook_tab_mains_details extends Activity {

protected TextView Name;
protected ImageView Photo;
protected TextView Ingredients;
protected TextView HowTo;
protected TextView Information;


protected int mainId;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cook_tab_generaldetails);

    mainId = getIntent().getIntExtra("MAINS_ID", 0);
    SQLiteDatabase db = (new Cook_tab_mains_data(this)).getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT emp._id, emp.name, emp.photo, emp.ingre, emp.howto, emp.info, emp.mainId, mgr.name mainsname, mgr.disc mainsdiscname FROM mains emp LEFT OUTER JOIN mains mgr ON emp.mainId = mgr._id WHERE emp._id = ?", 
            new String[]{""+mainId});

    if (cursor.getCount() == 1)
    {
        cursor.moveToFirst();

        Name = (TextView) findViewById(R.id.FoodName);
        Name.setText(cursor.getString(cursor.getColumnIndex("name")));

        Photo = (ImageView) findViewById(R.id.Photo);
        try {
            InputStream bitmap=getAssets().open(cursor.getString(cursor.getColumnIndexOrThrow("photo")));
            Bitmap bit=BitmapFactory.decodeStream(bitmap);
            Photo.setImageBitmap(bit);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        Ingredients = (TextView) findViewById(R.id.Ingre);
        Ingredients.setText(cursor.getString(cursor.getColumnIndex("ingre")));

        HowTo = (TextView) findViewById(R.id.HowtoDo);
        HowTo.setText(cursor.getString(cursor.getColumnIndex("howto")));

        Information = (TextView) findViewById(R.id.Information);
        Information.setText(cursor.getString(cursor.getColumnIndex("info")));



    } }}

The problem: I can’t seem to close() the database. I searched everywhere. I can’t find an answer.
Every time I try this command:

public void close() 
{
    Cook_tab_mains_data.close();
}

it gives me this error and won’t compile: Cannot make a static reference to the non-static method close() from the type SQLiteOpenHelper

So now what?
How do I close my database?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:13:52+00:00Added an answer on June 2, 2026 at 3:13 am

    At the very end of your onCreate method in activity Cook_tab_mains_details, add cursor.close(); and db.close() to close the cursor and database respectively.

    More preferably, you should load the data in onStart method and close the cursor+database in onStop method of your activity. (but thats just a suggestion)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making this app with a tableview and stuff. I want to make a
I am making app on android that take all data form device and Storage
I am making an app in Android wherein I require to move an image
I'm making my app shake-gesture-compatible by doing this in my UIViewController: - (void)viewWillAppear:(BOOL)animated {
I am making an app for android 3.0 and i need it to be
I'm making an app for 1.6 and I'm having a problem with android image
I' am making an app that connects to the database and they introduce the
I am making app that takes a screenshot of a URL requested by the
I am making app about google documents. but I don't know How to save
i m making APP which do image proceesing using java SE.i want to check

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.