SQLite Listview onclick 过滤器 db 以在新 activity 中打开结果
SQLite Listview onclick filters db to open result in new activity
我问了一个先前的问题 here 但我没有回答我正在尝试重组或使用不同的方法。
我有一个从 assets 文件夹中打开并复制的数据库,它工作正常,因为第一个 activity 从中打开并正确显示..
这个想法是,当你打开应用程序并调用class打开圣经时,它会打开这本书class并且有一个ListView,在listview中是所有的圣经书籍,单击时,它应打开章节activity,在其listView中显示所有书籍的所有章节,选择一章时,它应该打开经文Activity,在其listView中显示所有经文。[= 18 = 18 = ]
到目前为止,Activity这本书显示了书名,但是当我点击它时,它只显示白屏...
logcat.
中没有显示任何错误
我试过使用 Intent,但我无法让它工作,有人可以帮我解决这个问题吗?
也许我用错了意图?
这是代码,我想你们需要看看哪里有问题
请原谅我在命名时使用的南非荷兰语术语
从我的主要 activity 我使用:
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);
}
}
);
}
}
然后子activity:
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);
}
});
}
}
}
我的主要数据库类:
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;
}
}
我的子数据库类:
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;
}
}
正如我提到的,您需要对代码进行许多修改,但目前您可以通过以下更改使您的代码 运行:
首先在 DBHandlerHoofstuk 中更改您的 getListHoofstuk 方法 class
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 Activity By Below
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);
}
});
}
}
我问了一个先前的问题 here 但我没有回答我正在尝试重组或使用不同的方法。
我有一个从 assets 文件夹中打开并复制的数据库,它工作正常,因为第一个 activity 从中打开并正确显示..
这个想法是,当你打开应用程序并调用class打开圣经时,它会打开这本书class并且有一个ListView,在listview中是所有的圣经书籍,单击时,它应打开章节activity,在其listView中显示所有书籍的所有章节,选择一章时,它应该打开经文Activity,在其listView中显示所有经文。[= 18 = 18 = ]
到目前为止,Activity这本书显示了书名,但是当我点击它时,它只显示白屏... logcat.
中没有显示任何错误我试过使用 Intent,但我无法让它工作,有人可以帮我解决这个问题吗?
也许我用错了意图? 这是代码,我想你们需要看看哪里有问题 请原谅我在命名时使用的南非荷兰语术语
从我的主要 activity 我使用:
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);
}
}
);
}
}
然后子activity:
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);
}
});
}
}
}
我的主要数据库类:
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;
}
}
我的子数据库类:
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;
}
}
正如我提到的,您需要对代码进行许多修改,但目前您可以通过以下更改使您的代码 运行:
首先在 DBHandlerHoofstuk 中更改您的 getListHoofstuk 方法 class
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 Activity By Below
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);
}
});
}
}