我正在使用SQLite数据库来存储和检索我的Android应用程序的数据。在下面的代码中,我使用toast消息将表数据恢复到了我的屏幕。我想在textview中显示所有记录到我的屏幕。我无知在哪里做了改变。如果有人可以帮我解决这个问题,那将会非常有帮助。尝试了多个选项并失败了。
这是我的主要活动。
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();
}
}
提前致谢
答案 0 :(得分:0)
在TextView
中创建与textid类似的activity_main.xml
名称,然后在DisplayRecord中将Toast
更改为TextView
IN activity_main.xml
<TextView
android:id="@+id/textid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
IN MainActivity
public void DisplayRecord(Cursor cursor){
TextView text=(TextView)findViewById(R.id.textid);
text.setText(
"id:" + cursor.getString(0) + "\n" +
"Question:" + cursor.getString(1) + "\n" +
"Answer:" + cursor.getString(2)
);
}