【发布时间】:2019-07-20 08:57:57
【问题描述】:
我是 Android Studio 的新手,
我已经有包含 3 列(ID、标题、详细信息)的数据库文件
我想从数据库中创建一个包含“标题”的 ListView,当我单击其中一个标题时,它将前往下一个活动并显示我之前单击的标题中的“详细信息”。
数据库文件位于 assets 文件夹中。
我正在使用 Android Studio 最新版本,请帮助我获取 xml、java 和清单代码。谢谢。
这是我的代码,我只是成功地在 ListView 中显示了“标题”列,其余的我不知道该怎么做。
MainActivity.java
public class MainActivity extends AppCompatActivity {
static final String DBNAME = "story.db";
static final String DBASSETPATH = "databases/" + DBNAME;
static final String DBTABLE = "table";
static final String DBTITLE = "title";
static final String IDCOLUMN = "_id";
ListView mTableList;
SQLiteDatabase mDB;
SimpleCursorAdapter mSCA;
Cursor mCsr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTableList = (ListView) this.findViewById(R.id.storylist);
mDB = openStoryDB();
if (mDB != null) {
mCsr = mDB.query(DBTABLE,
new String[]{IDCOLUMN + " AS _id",
DBTITLE
},
null,null,null,null,null);
mSCA = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,mCsr,
new String[]{DBTITLE},
new int[]{android.R.id.text1},0);
mTableList.setAdapter(mSCA);
} else {
Toast.makeText(this,"Unable to open Database.",Toast.LENGTH_LONG);
}
}
private SQLiteDatabase openStoryDB() {
String dbpath = this.getDatabasePath(DBNAME).getPath();
if (this.getDatabasePath(DBNAME).exists()) {
Log.d("OPENSTORYDB","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("OPENSTORYDB","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("OPENSTORYDB","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("OPENSTORYDB","Failed to copy asset to DB");
e.printStackTrace();
return null;
}
return SQLiteDatabase.openDatabase(dbpath,null,SQLiteDatabase.OPEN_READWRITE);
}
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.book.story.MainActivity">
<ListView
android:id="@+id/storylist"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
【问题讨论】:
-
@Fantomas : 检查我编辑的问题
-
根据官方 Android 开发指南的建议,考虑将 Room 与 LiveData 结合使用,这里是一个示例:codelabs.developers.google.com/codelabs/android-persistence/#4
标签: android database sqlite listview android-sqlite