我正在尝试使用此代码制作列表视图
KUHAPDatabase.java
private static final String LABEL = "DatabaseUU";
public static final String PASAL = SearchManager.SUGGEST_COLUMN_TEXT_1;
public static final String ISI = SearchManager.SUGGEST_COLUMN_TEXT_2;
public static final String BAB = "BAB";
public static final String KATEGORI = "KATEGORI";
private static final String NAMA_DB = "undang";
static final String NAMA_TABEL = "kolom";
private static final int VERSI_DATABASE = 2;
private final DictionaryOpenHelper bukaDB;
private static final HashMap<String,String> untukHubungKolom = untukHubungKolom();
public KUHAPDatabase(Context context) {
bukaDB = new DictionaryOpenHelper(context);
}
private static HashMap<String,String> untukHubungKolom() {
HashMap<String,String> hubung = new HashMap<String,String>();
hubung.put(PASAL, PASAL);
hubung.put(ISI, ISI);
hubung.put(BAB, BAB);
hubung.put(KATEGORI, KATEGORI);
hubung.put(BaseColumns._ID, "rowid AS " +
BaseColumns._ID);
hubung.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, "rowid AS " +
SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID);
hubung.put(SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, "rowid AS " +
SearchManager.SUGGEST_COLUMN_SHORTCUT_ID);
return hubung;
}
public Cursor getWord(String brsID, String[] klm) {
String plhUU = "rowId = ?";
String[] kolomUU = new String[] {brsID};
return query(plhUU, kolomUU, klm);
}
public Cursor getWordMatches(String cariLokasiUU, String[] klm) {
String plhUU = PASAL + " MATCH ?";
String[] gudangUU = new String[] {cariLokasiUU+"*"};
return query(plhUU, gudangUU, klm);
}
private Cursor query(String pilih, String[] penyimpananUU, String[] klm) {
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(NAMA_TABEL);
builder.setProjectionMap(untukHubungKolom);
Cursor cursor = builder.query(bukaDB.getReadableDatabase(),
klm, pilih, penyimpananUU, null, null, null);
if (cursor == null) {
return null;
} else if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
return cursor;
}
private static class DictionaryOpenHelper extends SQLiteOpenHelper {
private final Context BukaDB;
private SQLiteDatabase DB;
private static final String FTS_TABLE_CREATE =
"CREATE VIRTUAL TABLE " + NAMA_TABEL +
" USING fts3 (" +
PASAL + ", " +
KATEGORI + ", " +
BAB + ", " +
ISI + ");";
DictionaryOpenHelper(Context bantuantukBukaDBUU) {
super(bantuantukBukaDBUU, NAMA_DB, null, VERSI_DATABASE);
BukaDB = bantuantukBukaDBUU;
}
@Override
public void onCreate(SQLiteDatabase buatDB) {
DB = buatDB;
DB.execSQL(FTS_TABLE_CREATE);
inputkanDBUU();
}
private void inputkanDBUU() {
new Thread(new Runnable() {
public void run() {
try {
inputkanUU();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
}
private void inputkanUU() throws IOException {
Log.d(LABEL, "Harap tunggu, sedang memasukan undang-undang kedalam database...");
final Resources asalUU = BukaDB.getResources();
InputStream inputUU = asalUU.openRawResource(R.raw.input_undang_undang);
BufferedReader bacaUU = new BufferedReader(new InputStreamReader(inputUU));
try {
String jajarUU;
while ((jajarUU = bacaUU.readLine()) != null) {
String[] daftarUU = TextUtils.split(jajarUU, "-");
if (daftarUU.length < 2) continue;
long uuID = inputUU(daftarUU[0].trim(), daftarUU[1].trim(), daftarUU[2].trim(), daftarUU[3].trim());
if (uuID < 0) {
Log.e(LABEL, "Maaf, tidak bisa menambah undang-undang: " + daftarUU[0].trim());
}
}
} finally {
bacaUU.close();
}
Log.d(LABEL, "Selesai menunggu undang.");
}
public long inputUU(String pasal, String isi, String bab, String kategori) {
ContentValues barisUU = new ContentValues();
barisUU.put(PASAL, pasal);
barisUU.put(ISI, isi);
barisUU.put(BAB, bab);
barisUU.put(KATEGORI, kategori);
return DB.insert(NAMA_TABEL, null, barisUU);
}
@Override
public void onUpgrade(SQLiteDatabase ubahDB, int versiDulu, int versiTerbaru) {
Log.w(LABEL, "Database di upgrade dari versi " + versiDulu + " ke "
+ versiTerbaru + ", dan akan menghapus semua data yg lama");
ubahDB.execSQL("DROP TABLE IF EXISTS " + NAMA_TABEL);
onCreate(ubahDB);
}
}
public Cursor fetchAllUndang(SQLiteDatabase db) {
return db.query("WISATA", new String[] { PASAL, ISI }, null, null,
null, null, null);
}
和这个
数据列表视图.java
private ArrayList<String> results = new ArrayList<String>();
private String tableName = KUHAPDatabase.NAMA_TABEL;
private SQLiteDatabase undang;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openAndQueryDatabase();
displayResultList();
}
private void displayResultList() {
TextView tView = new TextView(this);
tView.setText("This data is retrieved from the database and only 4 " +
"of the results are displayed");
getListView().addHeaderView(tView);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);
}
private void openAndQueryDatabase() {
try {
KUHAPDatabase dbHelper = new KUHAPDatabase(this.getApplicationContext());
undang = KUHAPDatabase.getWritableDatabase();
Cursor c = undang.rawQuery("SELECT BAB, KATEGORI FROM " +
tableName, null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String firstName = c.getString(c.getColumnIndex("BAB"));
String age = c.getString(c.getColumnIndex("KATEGORI"));
results.add("Name: " + firstName + ",KATEGORI: " + age);
}while (c.moveToNext());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
}
它导致错误。我希望我的列表视图从我的数据库中生成数据,我还是 android 的新手,有人可以帮忙吗?