我不擅長編程,我需要你的幫助。我只能在textview中顯示數據。它只顯示數據庫SQLite中的最後一個數據。在Listview中顯示來自SQLite的數據
這是我databasehelper
public class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.example.jathniel.myapplication/databases/";
private static String DB_NAME = "example.sqlite";
private SQLiteDatabase myDataBase;
private Context myContext = null;
public DatabaseHelper(Context context) {
super(context, DB_NAME, (SQLiteDatabase.CursorFactory) null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = this.checkDataBase();
if(!dbExist) {
this.getReadableDatabase();
try {
this.copyDataBase();
} catch (IOException var3) {
throw new Error("Error");
}
}
}
public void copyDataBase() throws IOException {
InputStream myInput = this.myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
FileOutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String e = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(e, (SQLiteDatabase.CursorFactory)null, 0);
} catch (SQLiteException var3) {
;
}
if(checkDB != null) {
checkDB.close();
}
return checkDB != null;
}
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
this.myDataBase = SQLiteDatabase.openDatabase(myPath, (SQLiteDatabase.CursorFactory)null, 0);
}
public synchronized void close() {
if(this.myDataBase != null) {
this.myDataBase.close();
}
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDatabase() {
}
這是dataDB
public class DataDB {
DatabaseHelper con;
String name,lastname;
public DataDB() {
}
public String getNameDB(Context context) throws SQLException {
this.con = new DatabaseHelper(context);
try {
this.con.createDataBase();
} catch (IOException var4) {
;
}
if (!this.con.checkDataBase()) {
return "ERROR";
} else {
this.con.openDataBase();
SQLiteDatabase db = this.con.getWritableDatabase();
for (Cursor cursor = db.rawQuery("SELECT name FROM exampleTable", (String[]) null); cursor.moveToNext(); this.name = cursor.getString(0)) {
}
this.con.close();
return this.name;
}
}
public String getLNameDB(Context context) throws SQLException {
this.con = new DatabaseHelper(context);
try {
this.con.createDataBase();
} catch (IOException var4) {
;
}
if(!this.con.checkDataBase()) {
return "ERROR";
} else {
this.con.openDataBase();
SQLiteDatabase db = this.con.getWritableDatabase();
for (Cursor cursor = db.rawQuery("SELECT lastname FROM exampleTable", (String[]) null); cursor.moveToNext(); this.lastname = cursor.getString(0)) {
}
this.con.close();
return this.lastname;
}
}
我有一個主要活動
public class MainActivity extends Activity {
DataDB data = new DataDB();
ListView list;
ArrayAdapter<String> listAdapter;
public MainActivity() {
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
ArrayList<String> name = new ArrayList<String>();
listAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, name);
try {
listAdapter.add(data.getNameDB(this));
} catch (SQLException e) {
e.printStackTrace();
}
}
使用'SimpleCursorAdapter' – pskink