SQLite Listview onclick筛选数据库以在新活动中打开结果 [英] SQLite Listview onclick filters db to open result in new activity
问题描述
我在此处问了一个先前的问题,但没有答案,我试图重组或使用其他方法.
I asked a previous question here but with no answer im trying to restructure or use a different approach.
我已经打开了一个数据库,并从assets文件夹复制了该数据库,因为第一个活动已打开并从中正确显示,所以该数据库可以正常工作.
I've got a DB that is opened and copied from the assets folder, this works correctly because the first activity opens and displays correctly from it..
这个想法是,当您打开应用程序并调用该类来打开圣经时,它将打开书本类并具有一个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.
我已经尝试过使用Intent,但是我无法使用它,有人可以帮助我吗?
I've tried using the Intent but I cant get it to work, Can someone please assist me with this?
也许我用错了意图? 这是我认为你们需要看看那里是否有问题的代码 请原谅我用南非荷兰语命名
Maybe I'm using the intent wrong? Here is the code I think you guys need to see if there's a problem somewhere Excuse the Afrikaans terms I use for naming
我使用的主要活动是:
public class BybelActivityBoek extends Activity {
private ListView listviewBybel;
private customAdapterBoektext adapter_customAdapterBoektext;
private List<defineBybeldbBoek> defineBybeldbBoekList;
private DBHandlerBoek DBHandlerBoek_DBHelperBoek;
public String boek_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.activity_bybel_boek);
listviewBybel = (ListView) findViewById(R.id.BybelBoekListView);
DBHandlerBoek_DBHelperBoek = new DBHandlerBoek(this);
//Check exists database
File Database = getApplicationContext().getDatabasePath(DBHandlerBoek.DBNAME);
if(false == Database.exists()){
DBHandlerBoek_DBHelperBoek.getReadableDatabase();
//Copy DB
if (DBHandlerBoek.copyDatabase(this)){
Toast.makeText(this, "Databasis Suksesvol", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(this, "Databasis Probleem", Toast.LENGTH_LONG).show();
return;
}
}
//Get bybel list in db when db exists
defineBybeldbBoekList = DBHandlerBoek_DBHelperBoek.getListBybel();
//Init adapter
adapter_customAdapterBoektext = new customAdapterBoektext(this, defineBybeldbBoekList);
//Set adapter for listview
listviewBybel.setAdapter(adapter_customAdapterBoektext);
//Listview item click listener
//BybelActivityHoofstuk will be launched by passing boek_id
listviewBybel.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener(){
@Override
public void onItemClick (AdapterView<?> arg0, View view, int arg2, long arg3){
//on selecting a book
//BybelHoofstukActivity will be launched to show hoofstukke inside
Intent boekIntent = new Intent(BybelActivityBoek.this, BybelActivityHoofstuk.class);
boekIntent.putExtra(boek_id, String.valueOf(arg3));
startActivity(boekIntent);
}
}
);
}
}
然后是子活动:
public class BybelActivityHoofstuk extends Activity {
private ListView listviewHoofstuk;
private customAdapterHoofstuktext adapter_customAdapterHoofstuktext;
private List<defineBybeldbHoofstuk> defineBybeldbHoofstukList;
private DBHandlerHoofstuk DBHandlerHoofstuk_DBHelper;
private SQLiteDatabase mDatabase;
ArrayList<HashMap<String, String>> HoofstukList;
//Boek id
String boek_id_vanaf_BybelActivityBoek;
@Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bybel_hoofstuk);
listviewHoofstuk = (ListView) findViewById(R.id.BybelHoofstukListView);
DBHandlerHoofstuk_DBHelper = new DBHandlerHoofstuk(this);
//Check exists database
File Database = getApplicationContext().getDatabasePath(DBHandlerBoek.DBNAME);
if(false == Database.exists()){
DBHandlerHoofstuk_DBHelper.getReadableDatabase();
//Get boek id
Intent boekIntent = getIntent();
boek_id_vanaf_BybelActivityBoek = boekIntent.getStringExtra("boek_id");
//hashmap for listview
HoofstukList = new ArrayList<HashMap<String, String>>();
//Set adapter for listview
listviewHoofstuk.setAdapter(adapter_customAdapterHoofstuktext);
//Get bybel list in db when db exists
defineBybeldbHoofstukList = DBHandlerHoofstuk_DBHelper.getListHoofstuk();
//Init adapter
adapter_customAdapterHoofstuktext = new customAdapterHoofstuktext(this,defineBybeldbHoofstukList);
listviewHoofstuk.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener(){
@Override
public void onItemClick (AdapterView<?> arg0, View view, int arg2, long arg3){
//on selecting single track get vers text
Intent hoofstukid = new Intent(getApplicationContext(),BybelActivityVers.class);
//to get vers hoofstuk_id is needed
String hoofstuk_id = ((TextView)view.findViewById(R.id.hoofstuk_id)).getText().toString();
hoofstukid.putExtra("hoofstuk_id", hoofstuk_id);
startActivity(hoofstukid);
}
});
}
}
}
我的主要DBClass:
public class defineBybeldbBoek extends AppCompatActivity{
public int _id;
private String _hebreeus;
private String _afrikaans;
public defineBybeldbBoek(int boek_id, String _hebreeus, String _afrikaans){
this._id = boek_id;
this._hebreeus = _hebreeus;
this._afrikaans = _afrikaans;
}
public int getboek_id() {
return _id;
}
public String get_hebreeus() {
return _hebreeus;
}
public String get_afrikaans() {
return _afrikaans;
}
}
我的子DBClass:
public class defineBybeldbHoofstuk extends AppCompatActivity{
private int hoofstuk_se_boek_id;
private int _id;
private int _hoofstuk;
public defineBybeldbHoofstuk(int hoofstuk_se_boek_id, int hoofstuk_id, int _hoofstuk){
this.hoofstuk_se_boek_id = hoofstuk_se_boek_id;
this._id = hoofstuk_id;
this._hoofstuk = _hoofstuk;
}
public int get_hoofstuk() {
return _hoofstuk;
}
public int hoofstuk_se_boek_id() {
return hoofstuk_se_boek_id;
}
public int get_id() {
return _id;
}
}
主要的DBHandler:
public class DBHandlerBoek extends SQLiteOpenHelper{
public static final int DATABASE_VERSION = 1;
public static final String DBNAME = "db name.db";
public static final String DBLOCATION = "correct db path here";
private Context mContext;
private SQLiteDatabase mDatabase;
public static final String TABLE_BOEK = "table_boek";
public static final String COLUMN_BOEK_ID = "_id";
public static final String COLUMN_BOEK_HEBREEUS = "_hebreeus";
public static final String COLUMN_BOEK_AFRIKAANS = "_afrikaans";
public static final String TABLE_HOOFSTUK = "table_hoofstuk";
public static final String COLUMN_HOOFSTUK_SE_BOEK_ID = "_id";
public DBHandlerBoek(Context context) {
super(context, DBNAME, null, DATABASE_VERSION);
this.mContext = context;
}
//Blank want db bestaan klaar
@Override
public void onCreate(SQLiteDatabase db) {
}
//When app gets installed, copy db to device when this activity runs
public static boolean copyDatabase(Context context){
try {
InputStream inputStream = context.getAssets().open(DBHandlerBoek.DBNAME);
String outFileName = DBHandlerBoek.DBLOCATION + DBHandlerBoek.DBNAME;
OutputStream outputStream = new FileOutputStream(outFileName);
byte[]buff = new byte[1024];
int length = 0;
while ((length = inputStream.read(buff)) > 0) {
outputStream.write(buff, 0, length);
}
outputStream.flush();
outputStream.close();
Log.w("BybelActivityBoek", "DB Copied");
return true;
}
catch (Exception e){
e.printStackTrace();
return false;
}
}
//blank want db word ekstern geupgrade
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
//maak db oop
public void opendatabase(){
String dbPath = mContext.getDatabasePath(DBNAME).getPath();
if (mDatabase !=null && mDatabase.isOpen()) {
return;
}
mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}
//maak db toe
public void closeDatabase(){
if (mDatabase!=null) {
mDatabase.close();
}
}
public List<defineBybeldbBoek> getListBybel(){
defineBybeldbBoek defineBybeldbBoek = null;
List<defineBybeldbBoek> defineBybelDBList = new ArrayList<>();
opendatabase();
Cursor cursor = mDatabase.rawQuery("SELECT * FROM table_boek", null);/*(die tabel se naam)*/
cursor.moveToFirst();
while (!cursor.isAfterLast()){
defineBybeldbBoek = new defineBybeldbBoek(cursor.getInt(0), cursor.getString(1),cursor.getString(2));
defineBybelDBList.add(defineBybeldbBoek);
cursor.moveToNext();
}
cursor.close();
closeDatabase();
return defineBybelDBList;
}
}
子DBHandler:
public class DBHandlerHoofstuk extends SQLiteOpenHelper{
public static final int DATABASE_VERSION = 1;
public static final String DBNAME = "db name.db";
public static final String DBLOCATION = "correct db path here";
private Context mContext;
private SQLiteDatabase mDatabase;
public static final String TABLE_HOOFSTUK = "table_hoofstuk";
public static final String COLUMN_HOOFSTUK_BOEK_ID = "hoofstuk_se_boek_id";
public static final String COLUMN_HOOFSTUK_ID = "_id";
public static final String COLUMN_HOOFSTUK = "_hoofstuk";
public DBHandlerHoofstuk(Context context) {
super(context, DBNAME, null, DATABASE_VERSION);
this.mContext = context;
}
//Blank want db bestaan klaar
@Override
public void onCreate(SQLiteDatabase db) {
}
//blank want db word ekstern geupgrade
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
//maak db oop
public void opendatabase(){
String dbPath = mContext.getDatabasePath(DBNAME).getPath();
if (mDatabase !=null && mDatabase.isOpen()) {
return;
}
mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}
//maak db toe
public void closeDatabase(){
if (mDatabase!=null) {
mDatabase.close();
}
}
public List<defineBybeldbHoofstuk> getListHoofstuk(){
defineBybeldbHoofstuk defineBybeldbHoofstuk = null;
List<defineBybeldbHoofstuk> defineBybeldbHoofstukList = new ArrayList<>();
opendatabase();
Cursor cursor = mDatabase.rawQuery("SELECT * FROM table_hoofstuk", null);/*(die tabel se naam)*/
cursor.moveToFirst();
while (!cursor.isAfterLast()){
defineBybeldbHoofstuk = new defineBybeldbHoofstuk(cursor.getInt(0), cursor.getInt(1),cursor.getInt(2));
defineBybeldbHoofstukList.add(defineBybeldbHoofstuk);
cursor.moveToNext();
}
cursor.close();
closeDatabase();
return defineBybeldbHoofstukList;
}
}
推荐答案
正如我提到的,您需要对代码进行很多修改,但是目前您可以通过以下更改来使代码运行:
As I mentioned you need to do many modifications in code, but currently You can make your code running by below changes :
首先在DBHandlerHoofstuk类中如下更改您的getListHoofstuk方法
public List<defineBybeldbHoofstuk> getListHoofstuk(String boek_id_vanaf_BybelActivityBoek)
{
defineBybeldbHoofstuk defineBybeldbHoofstuk = null;
List<defineBybeldbHoofstuk> defineBybeldbHoofstukList = new ArrayList<>();
opendatabase();
Cursor cursor = mDatabase.rawQuery("SELECT * FROM table_hoofstuk WHERE " + COLUMN_HOOFSTUK_BOEK_ID + " = '" + boek_id_vanaf_BybelActivityBoek + "'", null);/*(die tabel se naam)*/
cursor.moveToFirst();
while (!cursor.isAfterLast()){
defineBybeldbHoofstuk = new defineBybeldbHoofstuk(cursor.getInt(0), cursor.getInt(1),cursor.getInt(2));
defineBybeldbHoofstukList.add(defineBybeldbHoofstuk);
cursor.moveToNext();
}
cursor.close();
closeDatabase();
return defineBybeldbHoofstukList;
}
现在在下面更改BybelActivityHoofstuk活动
public class BybelActivityHoofstuk extends Activity
{
private ListView listviewHoofstuk;
private customAdapterHoofstuktext adapter_customAdapterHoofstuktext;
private List<defineBybeldbHoofstuk> defineBybeldbHoofstukList;
private DBHandlerHoofstuk DBHandlerHoofstuk_DBHelper;
private SQLiteDatabase mDatabase;
ArrayList<HashMap<String, String>> HoofstukList;
//Boek id
String boek_id_vanaf_BybelActivityBoek;
@Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bybel_hoofstuk);
listviewHoofstuk = (ListView) findViewById(R.id.BybelHoofstukListView);
DBHandlerHoofstuk_DBHelper = new DBHandlerHoofstuk(this);
//Check exists database
File Database = getApplicationContext().getDatabasePath(DBHandlerBoek.DBNAME);
if(false == Database.exists()){
DBHandlerBoek_DBHelperBoek.getReadableDatabase();
//Copy DB
if (DBHandlerBoek.copyDatabase(this)){
Toast.makeText(this, "Databasis Suksesvol", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(this, "Databasis Probleem", Toast.LENGTH_LONG).show();
return;
}
}
DBHandlerHoofstuk_DBHelper.getReadableDatabase();
//Get boek id
Intent boekIntent = getIntent();
boek_id_vanaf_BybelActivityBoek = boekIntent.getStringExtra("boek_id");
//hashmap for listview
HoofstukList = new ArrayList<HashMap<String, String>>();
//Get bybel list in db when db exists
defineBybeldbHoofstukList = DBHandlerHoofstuk_DBHelper.getListHoofstuk(boek_id_vanaf_BybelActivityBoek);
//Init adapter
adapter_customAdapterHoofstuktext = new customAdapterHoofstuktext(this,defineBybeldbHoofstukList);
//Set adapter for listview
listviewHoofstuk.setAdapter(adapter_customAdapterHoofstuktext);
listviewHoofstuk.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener(){
@Override
public void onItemClick (AdapterView<?> arg0, View view, int arg2, long arg3){
//on selecting single track get vers text
Intent hoofstukid = new Intent(getApplicationContext(),BybelActivityVers.class);
//to get vers hoofstuk_id is needed
String hoofstuk_id = ((TextView)view.findViewById(R.id.hoofstuk_id)).getText().toString();
hoofstukid.putExtra("hoofstuk_id", hoofstuk_id);
startActivity(hoofstukid);
}
});
}
}
这篇关于SQLite Listview onclick筛选数据库以在新活动中打开结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!