我问过上一个问题here,但我没有回答我试图重组或使用不同的方法。
我已经从资产文件夹中打开并复制了一个数据库,这可以正常工作,因为第一个活动打开并从中正确显示..
这个想法是当你打开应用程序并调用类来打开圣经时,它会打开book类并且有一个ListView,在listview中是所有的圣经书籍,当点击时,它应该打开章节活动和在ListView中显示所有书的章节,选择章节时应打开诗歌活动,并在ListView中显示所有经文。
到目前为止,书中的Activity显示了书名,但是当我点击它时,它只显示一个白色的屏幕... 没有任何东西在logcat中显示错误。
我尝试过使用Intent,但我无法让它工作,有人可以帮助我吗?
也许我使用了错误的意图? 以下是我认为您需要查看代码是否存在问题的代码 请原谅我用于命名的南非荷兰语术语
从我使用的主要活动开始:
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;
}
}
答案 0 :(得分:1)
正如我所提到的,您需要在代码中进行许多修改,但目前您可以通过以下更改来运行代码:
首先在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);
}
});
}
}