我想创建一个应用程序,显示从db文件到listview的文本但是它 显示错误
我的应用程序的这些类
package com.example.dell.externalfilestoragedatabasedemo.database;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by dell on 4/2/2017.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
String DB_PATH = null;
private static String DB_NAME = "lib.db";
private SQLiteDatabase myDatabase;
private final Context myContext;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
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 void copyDatabase() throws IOException {
InputStream is;
is = 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 = is.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
is.close();
}
private boolean checkDataBase() {
SQLiteDatabase checkDb = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLException e) {
}
if (checkDb != null) {
checkDb.close();
}
return checkDb != null ? true : false;
}
public void openDatabase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (myDatabase != null) {
myDatabase.close();
}
super.close();
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
if (newVersion > oldVersion) {
try {
copyDatabase();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public Cursor query(String table, String []columns, String selections, String []selectionArgs, String groupBy, String having, String orderBy){
return query(table,columns,selections,selectionArgs,groupBy,having,orderBy);
}
public Cursor query(String sql){
return query(sql);
}
}
MainActivity在这里:
package com.example.dell.externalfilestoragedatabasedemo;
import android.database.Cursor;
import android.database.SQLException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.dell.externalfilestoragedatabasedemo.constantsToUse.UsefulConstants;
import com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper;
import java.io.IOException;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private Cursor cousor;
String SQl="SELECT Topic FROM TopicTable";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.listView = (ListView) findViewById(R.id.listView);
copyDatabase();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
switch (itemValue){
case "Education":
UsefulConstants.tableConstant=01;
SQl="SELECT Educational_Field FROM Education";
break;
case "Story":
UsefulConstants.tableConstant=02;
break;
case "Health":
UsefulConstants.tableConstant=03;
break;
case "Competitive":
UsefulConstants.tableConstant=11;
break;
case "Commerce":
UsefulConstants.tableConstant=12;
break;
case "Biology":
UsefulConstants.tableConstant=13;
break;
case "Art":
UsefulConstants.tableConstant=14;
break;
case "Technical ":
UsefulConstants.tableConstant=15;
SQl="SELECT Technologies FROM Technical";
break;
}
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
}
private void copyDatabase(){
DatabaseHelper myDb= new DatabaseHelper(this);
try {
myDb.createDatabase();
UsefulConstants.databaseHelper=myDb;
} catch (IOException e) {
throw new Error("Unable to Create Database");
}
try {
myDb.openDatabase();
}
catch (SQLException e){
throw e;
}
Toast.makeText(this,"Success",Toast.LENGTH_LONG).show();
showListView();
}
private void showListView() {
Cursor cursor=UsefulConstants.databaseHelper.query(SQl);
if (cursor != null) {
Toast.makeText(this, "Done", Toast.LENGTH_LONG).show();
cursor.moveToFirst();
String[] data = new String[cursor.getCount()];
int i = 0;
for (; i < cursor.getCount(); i++) {
data[i] = cursor.getString(0);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, data);
// Assign adapter to ListView
listView.setAdapter(adapter);
} else if (cursor == null)
Toast.makeText(this, Boolean.toString(cursor != null), Toast.LENGTH_LONG).show();
}
}
显示错误:
com.example.dell.externalfilestoragedatabasedemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dell.externalfilestoragedatabasedemo, PID: 12227
java.lang.StackOverflowError: stack size 8MB
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.example.dell.externalfilestoragedatabasedemo.database.DatabaseHelper.query(DatabaseHelper.java:120)
at com.examp
le.dell.externalfiles
请帮我解决这个问题
答案 0 :(得分:1)
这部分代码存在问题:
.four
您从public Cursor query(String table, String []columns, String selections, String []selectionArgs, String groupBy, String having, String orderBy){
return query(table,columns,selections,selectionArgs,groupBy,having,orderBy);
}
方法返回相同的方法,并且您有无限递归。你应该返回别的东西