我正在使用 SQLiteStudio(v2.1.5) 来获取我的数据库 (dbName) 中的数据,并且我有 1 个表 (tbl_Names) 并放入 android。现在在我的数据库中,我有一个两列 id 和 name。在我的数据库(dbName)中,我输入了三个名称,包括(Abbygail、Andie 和 Alysa 等)。我只能在 TextView 中显示它。
这是我的完整代码:
String DB_PATH="";
SQLiteDatabase db;
File dbfile;
TextView txtBabyName1, txtBabyName2, txtBabyName3;
SQLiteDatabase writedb;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (android.os.Build.VERSION.SDK_INT >= 17) {
DB_PATH = getApplicationContext().getApplicationInfo().dataDir + "/databases/";
} else {
DB_PATH = "/data/data/" + getApplicationContext().getPackageName() + "/databases/";
}
File dbpath = new File(DB_PATH);
if (!dbpath.exists()) {
dbpath.mkdirs();
}
dbfile = new File(DB_PATH+"dbpath");
if (!dbfile.exists()) {
try {
InputStream is = getApplicationContext().getAssets().open("dbName");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
FileOutputStream fos = new FileOutputStream(dbfile);
fos.write(buffer);
fos.flush();
fos.close();
} catch (Exception e) { throw new RuntimeException(e);};
}
db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
txtBabyName1 = (TextView) findViewById(R.id.txtBabyName);
txtBabyName2 = (TextView) findViewById(R.id.txtMeaning);
txtBabyName3 = (TextView) findViewById(R.id.txtMeaningO);
Cursor cursor = db.rawQuery("Select * FROM tbl_Names", null);
TextView[] tvs = new TextView[]{txtBabyName1, txtBabyName2, txtBabyName3};
int i=0;
if (cursor.moveToFirst()) {
do {
tvs[i].setText(cursor.getString(1));
} while ((++i<3)&&(cursor.moveToNext()));
} else {
txtBabyName.setText("No data.");
}
}
问题:
如何在 android 中使用 ListView 显示我的所有数据?