SQLite ListView启动新活动的意图 [英] SQLite ListView start an Intent to a new Activity

查看:63
本文介绍了SQLite ListView启动新活动的意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过无数小时的观看YouTube视频和教程,并阅读了有关信息和建议之后,我得出的结论是,使用SQLite数据库开发圣经应用程序是必经之路.

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.

我现在已经创建了3个表格.
table_book,table_chapter和table_verse.
这些表位于一个数据库中.
单击第一个活动"以打开数据库时,数据库将使用oncreate进行安装.

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具有2x列_id,book_name
  • table_chapter具有3x列,即_id,id_of_book,章节编号
  • table_verse有4x列,_id,id_of_chapter,verse_number,verse_text

此外,

  • 我进行了3次活动,每个桌子1次
  • 我有3个DBClass,每个表1个
  • 我有3个DBHandler,每个表1个
  • 我有3个适配器,每个表1个

这个想法是,当您打开应用程序并调用该类来打开圣经时,它将打开书本类并具有一个ListView,在listview中是所有圣经书本,当单击时,应打开该章的活动并在其ListView中显示该书的所有章节,选择一个章节时,应打开该节的经文,并在ListView中显示所有经节.

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.

到目前为止,活动"一书显示了书名,但是当我单击它时,它仅显示白屏...
什么都不会在logcat中显示错误.

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.

我想我可能会在Activity将所选信息发送到新Activity来打开新ListView的方式上弄乱了?
该代码应如何显示?

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 );
   }
}
);

在活动"一章中:

//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);
   }
});

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;

推荐答案

到目前为止,书籍活动会显示书籍名称,但是当我单击它时,它只会显示白屏...

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?

所以,这样做

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

然后使用该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);

DBHelperChapter.getListChapter(book_id)可以返回此查询结果的位置

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

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

旁注:由于您使用的是SQLite数据库,因此我建议尝试尝试使用CursorAdapter而不是ArrayAdapter来加载ListView

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

这篇关于SQLite ListView启动新活动的意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆