1

After countless hours of watching YouTube videos and tutorials, and reading up on info and advice, I've come to the conclusion that doing a bible app by using a SQLite database is the way to go.

I've got no knowledge of coding whatsoever and just by taking bits and pieces of tutorial code and advice from someone I've got this far, so please be specific when answering.

I've now created 3x tables.
table_book, table_chapter, and table_verse.
The tables are in one database.
The database gets installed with oncreate when the first Activity is clicked to open.

  • table_book has 2x columns _id, book_name
  • table_chapter has 3x columns, _id, id_of_book, chapter_number
  • table_verse has 4x columns, _id, id_of_chapter, verse_number, verse_text

Furthermore,

  • I've got 3x Activities, 1 for each table
  • I've got 3x DBClasses, 1 for each table
  • I've got 3x DBHandlers, 1 for each table
  • I've got 3x Adapters, 1 for each table

The idea is that when you open the app and call the class to open the bible, it opens the book class and has a ListView, in the listview is all the bible books, when clicked, it should open the chapter activity and in its ListView display all the book's chapters, when selecting a chapter it should open the verse Activity and in its ListView display all the verses.

So far, the book Activity displays the book names, but when I click on it, it only displays a white screen...
Nothing shows errors in the logcat.

I think I might be messing something up in the way the Activity sends the selected info to the new Activity to open the new ListView?
How should this code look like?

Currently it's like this in the book class:

//Get bible list in db when db exists
DBClassBibledbBookList= DBHelperBook.getListBible();

//Init adapter
adapter_customAdapterBooktext = new customAdapterBooktext(this, DBClassBibledbBookList);

//Set adapter for listview
listviewBible.setAdapter(adapter_customAdapterBooktext);

   @Override
   public void onItemClick (AdapterView<?> arg0, View view, int arg2, long arg3){

       //on selecting a book
       //BookActivity will be launched to show chapters inside
       Intent bookid = new Intent (getApplicationContext(), ChapterActivity.class);

       //send book_id to ChapterActivity to get chapters under that book
       String book_id = ((TextView)view.findViewById(R.id.book_id)).getText().toString();
       bookid.putExtra("book_id", book_id);
       startActivity(bookid );
   }
}
);

And in the chapter Activity:

//Get bible list in db when db exists
DBClassBibledbChapterList = DBHelperChapter.getListChapter();

//Init adapter
adapter_customAdapterChaptertext = new customAdapterChaptertext (this,DBClassBibledbChapterList );

//Set adapter for listview
listviewChapter.setAdapter(customAdapterChaptertext );

//Get book id
Intent i = getIntent();
book_id = i.getStringExtra("book_id");

//hashmap for listview
ChapterList = new ArrayList<HashMap<String, String>>();

listviewChapter.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener(){
   @Override
   public void onItemClick (AdapterView<?> arg0, View view, int arg2, long arg3){
       //on selecting chapter get verse text
       Intent chapterid = new Intent(getApplicationContext(),BibleActivityVerse.class);
       //to get verse both book_id and chapter_id is needed
       String book_id = ((TextView) view.findViewById(R.id.book_id)).getText().toString();
       String chapter_id = ((TextView)view.findViewById(R.id.chapter_id)).getText().toString();

       chapterid.putExtra("book_id", book_id);
       chapterid.putExtra("chapter_id", chapter_id);

       startActivity(chapterid);
   }
});

The DBClass:

public List<DBClassBibledbChapter> getListChapter(){
        DBClassBibledbChapter DBClassBibledbChapter = null;
        List<DBClassBibledbChapter> DBClassBibledbChapterList = new ArrayList<>();
        opendatabase();
        Cursor cursor = mDatabase.rawQuery("SELECT * FROM table_chapter", null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()){
            DBClassBibledbChapter = new DBClassBibledbChapter (cursor.getInt(0), cursor.getInt(1),cursor.getInt(2));
            DBClassBibledbChapterList.add(DBClassBibledbChapter);
            cursor.moveToNext();
        }
        cursor.close();
        closeDatabase();
        return DBClassBibledbChapterList;
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
FDupie
  • 59
  • 12
  • Ive looked at other solutions and samples, and tried implementing them, but still no results – FDupie Sep 01 '16 at 04:54
  • show your getListChapter() method in db class – Vickyexpert Sep 01 '16 at 06:08
  • I edited the post @Vickyexpert – FDupie Sep 01 '16 at 16:05
  • You claim to have little to no experience in coding, but this is actually very easy to follow... yet, I don't understand the problem. You have started a `BibleActivityVerse` activity class, and passed in the `book_id` and `chapter_id`, so what is the problem? – OneCricketeer Sep 01 '16 at 16:11
  • So far, the book activity displays the book names, but when I click on it, it only displays a white screen when its suppose to open the new activity with new listview – FDupie Sep 01 '16 at 17:16
  • @FDupie First you need to change your getListChapter() method, as currently you have not put validation for selected book so it will fetch all chapters, second thing you need to pass book_id in this method and that you have to pass from your main code of chapter activity – Vickyexpert Sep 02 '16 at 03:58
  • @Vickyexpert to what must my getListChapter() method change to? And do you mean I must pass my book_id from the bookActivity to the chapterActivity? – FDupie Sep 03 '16 at 20:18
  • please see renewed question [here](http://stackoverflow.com/q/39320799/6781422) – FDupie Sep 06 '16 at 04:17
  • I asked a more detailed question here and got the answer: http://stackoverflow.com/a/39361223/6781422 – FDupie Sep 17 '16 at 21:06
  • I asked a more detailed question and got the answer: http://stackoverflow.com/a/39361223/6781422 – FDupie Sep 17 '16 at 21:08

2 Answers2

0

Just try to get the book_id from your list of the DBClassBibledbBookList arraylist on the onItemClick listener as below and then pass it to another activity. Also check you get the correct book id on your ChapterActivity and fetch the list of chapters from the table_chapter based on the book id from your database.

//Set adapter for listview
listviewBible.setAdapter(adapter_customAdapterBooktext);

   @Override
   public void onItemClick (AdapterView<?> arg0, View view, int arg2, long arg3){

       //on selecting a book
       //BookActivity will be launched to show chapters inside
       Intent bookid = new Intent (getApplicationContext(), ChapterActivity.class);

       //send book_id to ChapterActivity to get chapters under that book
       String book_id =  DBClassBibledbBookList.get(arg2).getbook_id().toString();
       bookid.putExtra("book_id", book_id);
       startActivity(bookid );
   }
});

Check out the String book_id in the above code.

Hope this will help you.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • could you please help me with code for the above? I inserted your line of code but im not sure how to get the value in the next activity... Do i still use the intent? @GrlsHu – FDupie Sep 01 '16 at 15:48
  • @GrlsHu please see renewed question [here](http://stackoverflow.com/q/39320799/6781422) – FDupie Sep 06 '16 at 04:18
0

So far, the book activity displays the book names, but when I click on it, it only displays a white screen...

I think I might be messing something up in the way the activity sends the selected info to the new activity to open the new listview? How should this code look like?

You probably want to get the chapters by a certain book, yes?

So, do this

//Get book id
Intent i = getIntent();
book_id = i.getStringExtra("book_id");

Then use that ID to query the database in a way that gets the chapters

//Get bible list in db when db exists
chapterList = DBHelperChapter.getListChapter(book_id);

//Init adapter
chapterAdapter = new customAdapterChaptertext (this, chapterList);

//Set adapter for listview
listviewChapter.setAdapter(chapterAdapter);

Where DBHelperChapter.getListChapter(book_id) could return the results of this query

mDatabase.rawQuery("SELECT * FROM table_chapter WHERE id_of_book = " + book_id);

Sidenote: since you are using a SQLite database, I may suggest trying to look into using a CursorAdapter rather than an ArrayAdapter for loading a ListView

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • This code still shows only white screen with the following in logcat: 09-01 18:48:30.295 1231-9756/? W/AudioFlinger: RecordThread: buffer overflow. I only see an Arraylist, but when i change to cursor i get a red line.. Or am i missing something? – FDupie Sep 01 '16 at 16:59
  • that logcat message is unrelated to your app... regarding the redline, a cursor is not a replacement for an arraylist, and I didnt say to change/replace, I simply stated that a CursorAdapter may be a better alternative, not that it would solve the problem – OneCricketeer Sep 01 '16 at 17:03
  • okay. Could you please help or tell me, ive got my db in DB Browser for sql, there im able to run queries and the results is what i want as a lstview, for example `select * from table_book where chapter_book_id = 2;` Now how would i go about to replace that 2 with whatever is passed from the previous activity? with the intent i know, but what would the code look like in both these activities? just to clarify, i basicly then run a new query on each activity which has an intend(from previous activity) in the query line.. – FDupie Sep 01 '16 at 17:14
  • Have you tried to write that query in Java? You seem to know what to write, so try `mDatabase.rawQuery("SELECT * FROM table_chapter WHERE id_of_book = " + book_id);` – OneCricketeer Sep 01 '16 at 17:17
  • Im a quick learner, and with countless tutorials and videos picked up how to run a select query ",) I'll try the code – FDupie Sep 01 '16 at 17:32
  • Personally, I prefer using the [`query()`](http://stackoverflow.com/questions/10600670/sqlitedatabase-query-method) method over `rawQuery` – OneCricketeer Sep 01 '16 at 17:36
  • I set this in my 1stActivity `//Get book id Intent i = getIntent(); book_id = i.getStringExtra("book_id");` then where do i set `mDatabase.rawQuery("SELECT * FROM table_chapter WHERE id_of_book = " + book_id);` ? in the 2ndActivity, or in the DBClass? When i set it to the activity, white screen, when i set it to the DBClass my 2ndActivity doesnt get called on click because i need to send the Intent to the DBClass, am i right? – FDupie Sep 01 '16 at 19:04
  • You send the `book_id` from the Activity1 to Activity2. In Activity2, use `getIntent()` to get that value. With that value, query the correct table. If you see no data, then maybe the database just doesn't have any data in it – OneCricketeer Sep 01 '16 at 19:34
  • Okay in my DBHandler, the cursor queries the db to get data, when I send the book_id to Activity2, how do I run a query from there as well? – FDupie Sep 01 '16 at 20:14
  • I've already said... Run the query on the some instance of one of your Database classes. (I don't know the exact difference between your naming conventions of DB class or DB handler). It might be beneficial for you to post another question with a [mcve] of the problem you are having. I feel like I've answered how to get the `book_id` value to the next activity and if you implemented this method of `DBHelperChapter.getListChapter(String book_id);`, then that should return the list of chapters. – OneCricketeer Sep 01 '16 at 21:45