SQLite
 sql >> Teknologi Basis Data >  >> RDS >> SQLite

SQLite Listview onclick memfilter db untuk membuka hasil dalam aktivitas baru

Seperti yang saya sebutkan, Anda perlu melakukan banyak modifikasi dalam kode, tetapi saat ini Anda dapat membuat kode Anda berjalan dengan perubahan di bawah ini :

Pertama Ubah metode getListChapter Anda seperti di bawah ini di kelas DBHandlerChapter

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

Sekarang Ubah Aktivitas BybelActivityChapter Anda Dengan Di Bawah

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



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Bagaimana Typeof() Bekerja di SQLite

  2. Tabel isi sebelumnya ActiveAndroid menggunakan migrasi skema

  3. SQLite DROP VIEW

  4. Bagaimana cara mengatur teks ke tombol secara acak dari SQLite tanpa pengulangan?

  5. Bagaimana cara membuat tabel menggunakan database sqlite di android?