データベースをすべてセットアップして実行しています。しかし、今私が遭遇した問題は、それをリストビューにロードすることです。サイトで他の質問を試しましたが、私の質問に対する回答はありません。初心者(らしい)の観点から。データベースをリストビューにロードするにはどうすればよいですか?
これは私のクラスにあるものです:(表示クラスは一番下にあります)
public class Inventory extends Activity {
DBAdapter db = new DBAdapter(this);
private Cursor cursor = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inventory_screen);
try {
String destPath = "/data/data/" + getPackageName()
+ "/databases/InventoryDB";
File f = new File(destPath);
if (!f.exists()) {
CopyDB(getBaseContext().getAssets().open("InventoryDB"),
new FileOutputStream(destPath));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
DBAdapter db = new DBAdapter(this);
try {
db.open();
Cursor c = db.getAllRecords();
if (c.moveToFirst()) {
do {
DisplayRecord(c);
} while (c.moveToNext());
}
db.close();
} catch (Exception e) {
Log.e("ERROR", "ERROR IN CODE:" + e.toString());
e.printStackTrace();
}
}
public void CopyDB(InputStream inputStream, OutputStream outputStream)
throws IOException {
// ---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
// List View
public void DisplayRecord(Cursor c) {
ListView InventoryItems = (ListView) findViewById(R.id.listViewInventory);
String[] Items = new String[] {c.getString(1)};
ArrayAdapter<String> ItemsAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, Items);
InventoryItems.setAdapter(ItemsAdapter);
//Toast msg = Toast.makeText(Inventory.this, "id: " + c.getString(0)
// + "\n" + "Item: " + c.getString(1) + "\n", Toast.LENGTH_SHORT);
//msg.show();
}
データベースの最初のアイテムを取得するようにリストビューを設定しました(私の知識はそれだけです)
すべてのデータを表示するにはどうすればよいですか? simpleCursorAdapter について聞いたことがありますが、それが関連しているかどうかわかりませんか?