SQLite ListView启动Intent到一个新的活动

时间:2016-09-01 04:53:21

标签: android sqlite listview android-intent android-sqlite

经过无数个小时观看YouTube视频和教程,阅读信息和建议后,我得出的结论是,使用SQLite数据库做一个圣经应用程序是可行的。

我对编码没有任何了解,只是通过从我已经到目前为止的人那里获取一些教程代码和建议,所以请在回答时具体说明。

我现在已经创建了3x桌子 table_book,table_chapter和table_verse 这些表位于一个数据库中 单击第一个Activity打开时,数据库将使用oncreate进行安装。

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

此外,

  • 我有3次活动,每张桌子1次
  • 我有3个DBClasses,每个表1个
  • 我有3个DBHandlers,每个表1个
  • 我有3个适配器,每个表1个

这个想法是当你打开应用程序并调用类来打开圣经时,它会打开book类并且有一个ListView,在listview中是所有的圣经书籍,当点击时,它应该打开章节活动和在ListView中显示所有书的章节,选择章节时应打开诗歌活动,并在ListView中显示所有经文。

到目前为止,书籍活动显示了书名,但是当我点击它时,它只显示一个白色的屏幕......
没有任何东西在logcat中显示错误。

我想我可能会在Activity将所选信息发送到新Activity以打开新ListView的方式搞乱一些事情?
这段代码应该怎么样?

目前在书类中它是这样的:

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

2 个答案:

答案 0 :(得分:0)

尝试从onItemClick侦听器的book_id arraylist列表中获取DBClassBibledbBookList,如下所示,然后将其传递给另一个活动。 另外,检查您在ChapterActivity上是否获得了正确的图书ID,并根据您数据库中的图书ID从table_chapter中获取章节列表。

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

在上面的代码中查看String book_id

希望这会对你有所帮助。

答案 1 :(得分:0)

  

到目前为止,图书活动显示了图书名称,但是当我点击它时,它只显示一个白色的屏幕......

     

我想我可能会在活动将所选信息发送到新活动以打开新列表视图的方式搞乱一些事情?这段代码应该怎么样?

您可能希望通过某本书获得章节,是吗?

所以,这样做

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

然后使用该ID以获取章节的方式查询数据库

//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)可以返回此查询的结果

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

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