【发布时间】:2013-04-30 14:02:33
【问题描述】:
我对 Android 还很陌生。我正在制作一个应用程序,该应用程序在先前和向前按钮单击时显示大量不同的刺痛。我还有一个存储在资产文件夹中的数据库。现在我对如何在 Ecilipse 中从该数据库中读取 tdo 并将其传递到 textview 感到有些困惑。另外,是否可以一一显示字符串。就像它发生在数组中一样?
DataBaseCreator.java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class DataBaseCreator extends SQLiteOpenHelper {
private static String DB_PATH = "/GetInspiredAndHaveFun/assets/Jokes.db";
private static String DB_NAME = "Jokes.db";
private static int DB_VERSION = 1;
public static String DATABASE_TABLE = "myjoke";
public String temp = "";
public String[] col;
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseCreator(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
}
public void openDatabase() throws IOException {
boolean dbe = checkDataBase();
if (dbe) {
Log.i("Tag", "dbe" + dbe);
} else {
Log.i("Tag", "dbdoesnotexist" + dbe);
this.getReadableDatabase();
copyDataBase();
}
}
public void copyDataBase() throws IOException {
InputStream inp = myContext.getAssets().open(DB_NAME);
String ofn = DB_PATH + DB_NAME;
OutputStream oup = new FileOutputStream(ofn);
byte[] buffer = new byte[1024];
int length;
while ((length = inp.read(buffer)) > 0) {
oup.write(buffer, 0, length);
}
oup.flush();
inp.close();
oup.close();
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
catch (SQLiteException e) {
}
return checkDB != null ? true : false;
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
passText();
}
public final String passText() {
Cursor cursor = myDataBase.query(DATABASE_TABLE, col, null, null,
null, null, null);
String[] column = cursor.getColumnNames();
column=col;
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
temp += cursor.getString(50);
cursor.moveToNext();
}
return temp;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
笑话活动
import java.io.IOException;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class Jokes extends Activity {
ImageButton bck, fwd, cpy, col;
TextView t1;
String[] jok;
int i, a3;
TextView nm1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jokes);
..
...
...
...
..
.
}
{
DataBaseCreator myDbHelper = new DataBaseCreator(this);
try{
myDbHelper.copyDataBase();
}
catch (IOException ioe) {
throw new Error("Unable to create database");
}
myDbHelper.openDataBase();
}
}
【问题讨论】:
-
你能在你的代码中展示你到目前为止做了什么吗?所以我可以指导你。
-
我编辑了我的帖子。看看吧。
-
您能否详细说明您想要进入的流程。请给我一个概述,您的代码应该如何工作。
-
我想要它做的是读取所有内容,即我存储在数据库中的所有字符串。然后,当用户按下前进按钮时,它会显示数据库中的第一个条目。在上一个按钮上单击它会显示数据库中的上一个条目。简单的!然而复杂。
-
如果我没记错的话,我理解的是,当用户点击按钮时,它应该转到下一个活动,在该活动中显示一个文本视图(用于显示字符串)和两个按钮(上一个和下一个显示上一个和下一个字符串)。我说的对吗?
标签: java android coding-style android-sqlite