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 7077449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:25:29+00:00 2026-05-28T06:25:29+00:00

I’m working on my first android application and running into an issue I can’t

  • 0

I’m working on my first android application and running into an issue I can’t seem to find a solution for. I’m trying to setup my UI with a series of listviews that will at the end connect to a page displaying edit text that will require input. IE Categories -> Sources -> Titles -> Edittexts our in other words, they would first be displayed a list of categories (Music, Movies etc). Then the user clicks on lets say Music, it would then list artists in my database. Followed by clicking on an artist it would display their songs in my database. I have it setup atm where it will display my cursors I’ve setup but I’m having trouble setting up a my program to see what the user clicked and give results/display cursor that has the data I want to show.

public class dbadapter extends SQLiteOpenHelper {

     //The Android's default system path of your application database.
   public static final String DB_PATH = "/data/data/wanted.pro.madlibs/databases/";
   public static final String DB_NAME = "madlib";
   public static final int DB_VERSION = 1;
   private static final String TAG = "dbadapter";

   //database variables
   public static final String  KEY_ID = "_id";
   public static final String  KEY_CATEGORYDESC = "categorydesc";
   public static final String KEY_TITLE = "titlekey";
   public static final String KEY_TITLEDESC = "titledesc";
   public static final String KEY_TITLESTORY = "titlestory";
   public static final String KEY_SOURCEDESC = "sourcedesc";
   public static final String KEY_SOURCE = "sourcekey";
   public static final String KEY_CATEGORY = "categorykey";

   //table variables
   public static final String CATEGORY_TABLE = "category";
   public static final String SOURCE_TABLE = "source";
   public static final String TITLE_TABLE = "title";

   private dbadapter mydbhelper;
   private static SQLiteDatabase myDataBase; 


   @Override
   public void onOpen(SQLiteDatabase myDatabase)
   {
     super.onOpen(myDatabase);
     if (!myDatabase.isReadOnly())
     {
       // Enable foreign key constraints
       myDatabase.execSQL("PRAGMA foreign_keys=ON;");
     }
   }

   /**
    * Constructor
    * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
    * @param context
    */

       private final Context mCtx;
    public dbadapter(Context context) {

    super(context, DB_NAME, null, DB_VERSION);
       this.mCtx = context;
   }    

 /**
    * Creates a empty database on the system and rewrites it with your own database.
    * */
   public void createDataBase() throws IOException{

    boolean dbExist = checkDataBase();

    if(dbExist){
        //do nothing - database already exist
    }else{

        //By calling this method and empty database will be created into the default system path
              //of your application so we are gonna be able to overwrite that database with our database.
        this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }


   }

   /**
    * Check if the database already exist to avoid re-copying the file each time you open the application.
    * @return true if it exists, false if it doesn't
    */
   private boolean checkDataBase(){

    SQLiteDatabase checkDB = null;

    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }catch(SQLiteException e){

        //database does't exist yet.

    }

    if(checkDB != null){

        checkDB.close();

    }

    return checkDB != null ? true : false;
   }

   /**
    * Copies your database from your local assets-folder to the just created empty database in the
    * system folder, from where it can be accessed and handled.
    * This is done by transferring bytestream.
    * */
   private void copyDataBase() throws IOException{

    //Open your local db as the input stream
    InputStream myInput = mCtx.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

   }

   public void openDataBase() throws SQLException{

    //Open the database
       String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

   }

   @Override
    public synchronized void close() {

        if(myDataBase != null)
            myDataBase.close();

        super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS category");
        onCreate(db);
    }

    public dbadapter open() throws SQLException {
        mydbhelper = new dbadapter (mCtx);
        myDataBase = mydbhelper.getWritableDatabase();
        return this;
    }



    // Add your public helper methods to access and get content from the database.
      // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
      // to you to create adapters for your views.

    // retrieves all the categories
      public static Cursor getAllCategories() 
        {
            return myDataBase.query(CATEGORY_TABLE, new String[] {
                    KEY_ID, KEY_CATEGORY,
                    KEY_CATEGORYDESC,
                    }, 
                    null, null, null, null, KEY_CATEGORYDESC);

        }
    // retrieves all the titles
          public Cursor getAllTitles() 
            {
                return myDataBase.query(TITLE_TABLE, new String[] {
                        KEY_ID, 
                        KEY_TITLE,
                        KEY_TITLEDESC,
                        KEY_TITLESTORY,
                        }, 
                        null, null, null, null, KEY_TITLEDESC);

            }
        // retrieves all the sources
          public  Cursor getAllSources() 
            {
                return myDataBase.query(SOURCE_TABLE, new String[] {
                        KEY_ID, 
                        KEY_SOURCE,
                        KEY_SOURCEDESC,
                        }, 
                        null, null, null, null, KEY_SOURCEDESC);

            }

Here is the code from my adapter

public class categories extends ListActivity {
    /** Called when the activity is first created. */
    private dbadapter mydbhelper;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.category_list);
        mydbhelper = new dbadapter(this);
        mydbhelper.open();
        fillData();
    }
    private void fillData() {
            Cursor c = mydbhelper.getAllCategories();
            startManagingCursor(c);

     // Create an array to specify the fields we want to display in the list (TITLE,DATE,NUMBER)
        String[] from = new String[] {dbadapter.KEY_CATEGORYDESC};

        // an array of the views that we want to bind those fields to (in this case text1,text2,text3)
        int[] to = new int[] {R.id.text1};

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter adapter = 
            new SimpleCursorAdapter(this, R.layout.cate_row, c, from, to);
        setListAdapter(adapter);
        }
@Override
protected void onListItemClick(ListView list, View v, int position, long id)
{
    super.onListItemClick(list, v, position, id);
    final Intent intent = new Intent(this, source.class);

    startActivityForResult(intent, position);
    }
}

this would be my sql log from where I made my database tables

CREATE TABLE category(
_id INT,  
categorykey INT,
  categorydesc TEXT,
   PRIMARY KEY(_id)
);
CREATE TABLE source(
_id INT,  
sourcekey INT,
  sourcedesc TEXT,
   PRIMARY KEY(_id)
);
CREATE TABLE title(
_id INT,  
titlekey INT,
  titledesc TEXT,
titlestory TEXT,
   PRIMARY KEY(_id)
);
CREATE TABLE userword(
_id INT,  
titlekey INT,
  userword TEXT,
   PRIMARY KEY(_id), FOREIGN KEY(titlekey) REFERENCES title(titlekey)
);
CREATE TABLE category_title(
titlekey INT,
  categorykey INT,
  FOREIGN KEY(titlekey) REFERENCES title(titlekey), FOREIGN KEY (categorykey) REFERENCES category(categorykey)
);
CREATE TABLE source_title(
titlekey INT,
  sourcekey INT,
  FOREIGN KEY(titlekey) REFERENCES title(titlekey), FOREIGN KEY (sourcekey) REFERENCES source(sourcekey)
);
CREATE TABLE source_category(
sourcekey INT,
  categorykey INT,
  FOREIGN KEY(categorykey) REFERENCES category(categorykey), FOREIGN KEY (sourcekey) REFERENCES source(sourcekey)
);

The only cursor I know I have right is the Category, since it’s the only data in database table that will not be filtered by previous selected.

  • 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-05-28T06:25:30+00:00Added an answer on May 28, 2026 at 6:25 am

    If I got your scenario right you are wondering how to determine which object (specifically which entry) has been selected. This can be done pretty straight forward, Bill Gary gave a hint. There are two options:

    1. Use the “position” as an indicator where to move with your
      current curso. based on your current cursor and the position you an
      then query one layer deeper.
    2. Use the “Proxy Pattern” to make a clear distinction between your
      logic and database data. I do it this way. You create a new class
      which represents a database entry, for example the categories.
      Rather than filling your adapter wit pure database data insert those
      objects (the so called proxy). Those proxy’s have a reference to
      their corresponding database entry (via the cursor). When clicking
      an item you can just ask the adapter for the item at this place.
      then you can ask this item your the referenced data entry. Then you
      query your database in the next layer (artist…) for that dataset.

    For the correct query you should use a SELECT … WHERE statement which you can easily implement with the query() method. But as your using this method i think you know how to do this.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.