所以我想创建一个应用程序,列出我存储在我创建的现有SQLite数据库中的食物。我的foodDB.db文件位于/ assets / databases文件夹中。
我知道我必须使用DatabaseHelper类。我已经完成了相当多的研究,我已经查看了大量的stackoverflow帖子和youtube教程,但没有一个满足我从资产文件夹中的现有数据库填充ListView的条件。我觉得它们很混乱而且不清楚。
有人可以清楚地向我解释我将如何解决这个问题吗?我真的很挣扎,并希望得到一些指导。
我的数据库名为foodDatabase.db
我的数据库包含一个名为dataset
的表
表格dataset
包含列:id, name, description, protein, fat, carbohydrates, energy, starch, sugar, cholesterol
。
答案 0 :(得分:0)
首先要注意几点。
1 - 你说文件是 FoodDB.db ,但后来说数据库名为 foodDatabase.db 。数据库名称是文件名。因此,以下示例使用 FoodDB.db 作为资产中的文件(您可以在从资产复制时重命名该文件,但我在此答案中没有这样做。)
2 - 您不需要数据库帮助程序,示例不使用数据库帮助程序。
分为两部分: -
1)访问数据库以提取ListView的数据。在以下示例中,将其从资产复制到标准数据库位置( / data / data / the_package / databases / the_database_name )。
2)在ListView中显示提取的数据(作为光标获得)。
要执行2,您需要布局以包含ListView,因此使用了以下布局。 activity_main.xml : -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fooddb.so49328656populatelistview.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
<ListView
android:id="@+id/foodlist"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
tools:context="fooddb.so49328656populatelistview.MainActivity"
必须反映您的包 数据库助手 - 未用作警告
调用活动 MainActivity.java (见注释): -
public class MainActivity extends AppCompatActivity {
static final String DBNAME = "FoodDB.db";
static final String DBASSETPATH = "databases/" + DBNAME;
static final String FOODTABLE = "dataset";
static final String FOODCOLUMN = "Food";
static final String IDCOLUMN = "ID";
ListView mFoodList;
SQLiteDatabase mDB;
SimpleCursorAdapter mSCA;
Cursor mCsr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFoodList = (ListView) this.findViewById(R.id.foodlist);
mDB = openFoodDB();
if (mDB != null) {
mCsr = mDB.query(FOODTABLE,
new String[]{IDCOLUMN + " AS _id",
FOODCOLUMN
},
null,null,null,null,null);
mSCA = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,mCsr,
new String[]{FOODCOLUMN},
new int[]{android.R.id.text1},0);
mFoodList.setAdapter(mSCA);
} else {
Toast.makeText(this,"Unable to open Database.",Toast.LENGTH_LONG);
}
}
private SQLiteDatabase openFoodDB() {
String dbpath = this.getDatabasePath(DBNAME).getPath();
if (this.getDatabasePath(DBNAME).exists()) {
Log.d("OPENFOODDB","Opening already existing Database");
return SQLiteDatabase.openDatabase(dbpath,null,SQLiteDatabase.OPEN_READWRITE);
}
InputStream is;
byte[] buffer;
FileOutputStream db;
try {
is = this.getAssets().open(DBASSETPATH);
buffer = new byte[is.available()];
is.read(buffer);
is.close();
} catch (Exception e) {
e.printStackTrace();
Log.d("OPENFOODDB","Unable to locate or buffer input from assets " + DBASSETPATH);
return null;
}
// Just in case the databases directory doesn't exist create it.
File dbmkdir = (this.getDatabasePath(DBNAME)).getParentFile();
dbmkdir.mkdirs();
try {
db = new FileOutputStream(this.getDatabasePath(DBNAME).getPath());
} catch (Exception e) {
e.printStackTrace();
Log.d("OPENFOODDB","Unable to create outputstream for DB at path " + dbpath);
try {
is.close();
} catch (Exception e2) {
}
return null;
}
try {
db.write(buffer);
db.flush();
db.close();
is.close();
} catch (Exception e) {
Log.d("OPENFOODDB","Failed to copy asset to DB");
e.printStackTrace();
return null;
}
return SQLiteDatabase.openDatabase(dbpath,null,SQLiteDatabase.OPEN_READWRITE);
}
}
如果数据库不存在,openFoodDB
方法从资产文件复制后返回SQLiteDatabase。如果有问题,该方法将返回null。
D/OPENFOODDB: Opening already existing Database
D/OPENFOODDB: Unable to locate or buffer input from assets databases/FoodDB.db
这将在堆栈跟踪之前显示e.g。 : -
03-16 22:17:04.008 1529-1529/? W/System.err: java.io.FileNotFoundException: databases/FoodDB.db
03-16 22:17:04.008 1529-1529/? W/System.err: at android.content.res.AssetManager.openAsset(Native Method)
03-16 22:17:04.008 1529-1529/? W/System.err: at android.content.res.AssetManager.open(AssetManager.java:315)
03-16 22:17:04.008 1529-1529/? W/System.err: at android.content.res.AssetManager.open(AssetManager.java:289)
03-16 22:17:04.008 1529-1529/? W/System.err: at fooddb.so49328656populatelistview.MainActivity.openFoodDB(MainActivity.java:63)
03-16 22:17:04.008 1529-1529/? W/System.err: at fooddb.so49328656populatelistview.MainActivity.onCreate(MainActivity.java:37)
03-16 22:17:04.008 1529-1529/? W/System.err: at android.app.Activity.performCreate(Activity.java:5008)
03-16 22:17:04.008 1529-1529/? W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-16 22:17:04.008 1529-1529/? W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-16 22:17:04.008 1529-1529/? W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-16 22:17:04.008 1529-1529/? W/System.err: at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-16 22:17:04.008 1529-1529/? W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-16 22:17:04.008 1529-1529/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 22:17:04.008 1529-1529/? W/System.err: at android.os.Looper.loop(Looper.java:137)
03-16 22:17:04.008 1529-1529/? W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4745)
03-16 22:17:04.008 1529-1529/? W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
03-16 22:17:04.008 1529-1529/? W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
03-16 22:17:04.008 1529-1529/? W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-16 22:17:04.008 1529-1529/? W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-16 22:17:04.008 1529-1529/? W/System.err: at dalvik.system.NativeStart.main(Native Method)
IDCOLUMN + " AS _id"
。