我正在使用SQLite數據庫來存儲和檢索數據到我的android應用程序。在下面的代碼中,我使用Toast消息將我的表格數據添加到我的屏幕上。我想在textview中將所有記錄顯示到我的屏幕上。我很無奈在哪裏做改變。如果有人可以用這段代碼來幫助我,那真的很有幫助。嘗試了多個選項並失敗。在文本視圖中顯示Toast消息
這是我的主要活動。
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHandler db = new DatabaseHandler(this);
try {
String destPath = "/data/data/" + getPackageName()
+ "/databases/questions";
File f = new File(destPath);
if (!f.exists()) {
CopyDB(getBaseContext().getAssets().open("questions"),
new FileOutputStream(destPath));
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Error File", Toast.LENGTH_SHORT)
.show();
}
catch (IOException e1) {
e1.printStackTrace();
Toast.makeText(MainActivity.this, "Error IO", Toast.LENGTH_SHORT)
.show();
}
Cursor cursor = db.getAllQuestions();
if (cursor.moveToFirst())
{
do {
DisplayRecord(cursor);
}
while(cursor.moveToNext());
}
db.close();
}
public void CopyDB(InputStream inputstream, OutputStream outputstream)
throws IOException {
byte[] buffer = new byte[1024];
int length;
while ((length = inputstream.read(buffer))>0){
outputstream.write(buffer, 0, length);
}
inputstream.close();
outputstream.close();
}
public void DisplayRecord(Cursor cursor){
Toast.makeText(this,
"id:" + cursor.getString(0) + "\n" +
"Question:" + cursor.getString(1) + "\n" +
"Answer:" + cursor.getString(2),
Toast.LENGTH_SHORT).show();
}
}
在此先感謝
您可能想使用對話框而不是烤麪包 –