【发布时间】:2018-11-21 02:58:58
【问题描述】:
我是 Android Studio 的新手。我有一个包含三列的 SQLite 数据库,我会将所有数据显示到列表视图中。该数据库位于资产文件夹中。我尝试在片段类中使用 SimpleCursorAdapter 但没有成功。当我在我的设备上启动它时应用程序崩溃。谁能告诉我为什么?
这是我的片段类:
public class rightFragment extends Fragment {
View view;
Cursor c;
SimpleCursorAdapter adapter;
ListView listView;
Button buttondisplay;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate (R.layout.fragment_right, container, false);
DatabaseHelper myDbHelper = new DatabaseHelper (getActivity ());
try {
myDbHelper.createDataBase ();
} catch (IOException ioe) {
}
try {
myDbHelper.openDataBase ();
} catch (SQLException sqle) {
throw sqle;
}
c = myDbHelper.query ("fradeu", null, null, null, null, null, null);
Cursor c = myDbHelper.readData ();
String[] from = new String []{DatabaseHelper.COL1,DatabaseHelper.COL2};
int[] to = new int[] {R.id.fra, R.id.deu};
adapter = new SimpleCursorAdapter (getActivity (), R.layout.list_view_adapter, c, from, to) {
};
buttondisplay.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
}
});
return view
}
}
数据库助手:
public class DatabaseHelper extends SQLiteOpenHelper {
String DB_PATH = null;
private static String DB_NAME = "mydatabase3.db";
private SQLiteDatabase database;
private final Context myContext;
public static final String TABLE_NAME = "fradeu";
public static final String COL1 = "fra";
public static final String COL2 = "deu";
public DatabaseHelper(Context context) {
super (context, DB_NAME, null, 10);
this.myContext = context;
this.DB_PATH = "/data/data/" + context.getPackageName () + "/" + "databases/";
Log.e ("Path 1", DB_PATH);
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase ();
if (dbExist) {
} else {
this.getReadableDatabase ();
try {
copyDataBase ();
} catch (IOException e) {
throw new Error ("Error copying database");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase (myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
}
if (checkDB != null) {
checkDB.close ();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets ().open (DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream (outFileName);
byte[] buffer = new byte[10];
int length;
while ((length = myInput.read (buffer)) > 0) {
myOutput.write (buffer, 0, length);
}
myOutput.flush ();
myOutput.close ();
myInput.close ();
}
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
database = SQLiteDatabase.openDatabase (myPath, null, SQLiteDatabase.OPEN_READONLY);
}
public Cursor readData(){
String[] allColumns = new String[] {DatabaseHelper.COL1,DatabaseHelper.COL2};
Cursor c =database.query (DatabaseHelper.TABLE_NAME,allColumns,null,null,null,null,null);
if(c != null) {
c.moveToFirst ();
}
return c;
}
@Override
public synchronized void close() {
if (database != null)
database.close ();
super.close ();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion)
try {
copyDataBase ();
} catch (IOException e) {
e.printStackTrace ();
}
}
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
return database.query ("fradeu", null, null, null, null, null,null);
}
}
用于 SimpleCursorAdapter 的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/fra"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fra"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/deu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="deu"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
【问题讨论】:
标签: android sqlite android-fragments assets simplecursoradapter