SQLite ListView start an Intent to a new Activity

2019-08-01 04:52发布

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;

2条回答
SAY GOODBYE
2楼-- · 2019-08-01 05:20

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

查看更多
啃猪蹄的小仙女
3楼-- · 2019-08-01 05:25

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.

查看更多
登录 后发表回答