SQLite Listview onclick filters db to open result

2019-02-20 21:29发布

I asked a previous question here but with no answer im trying to restructure or use a different approach.

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..

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.

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.

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

From my main activity I use:

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

And then sub 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);
        }
    });
}
 }
}

My main 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;
}

}

My sub 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;
}

}

The main 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;
}
}

The sub 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;
}
}

1条回答
混吃等死
2楼-- · 2019-02-20 22:12

As I mentioned you need to do many modifications in code, but currently You can make your code running by below changes :

First Change Your getListHoofstuk method as below in DBHandlerHoofstuk 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;
}

Now Change Your 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);
            }
        });
    }
}
查看更多
登录 后发表回答