我需要帮助,我试图在android studio上的TextView上显示数据库中的信息,这是学生姓名及其年龄的列表。我知道如何显示随机文本(注释掉的行),但是不知道何时使用数据库
public void doList(View v){
LinearLayout l = (LinearLayout)findViewById(R.id.mainView);
Cursor c = myDB.doQuery("SELECT name, age from students");
while (c.moveToNext()){
TextView t = new TextView(this);
//t.setText("Some text");
//l.addView(t);
System.out.println(c.getString(c.getColumnIndex("name")) + "," + c.getLong(c.getColumnIndex("age")));
}
c.close();
}
完整代码(MainActivity.java):
package com.example.databaseexample;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
DatabaseHelper myDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myDB=new DatabaseHelper(this, "app");
setContentView(R.layout.activity_main);
}
public void doList(View v){
LinearLayout l = (LinearLayout)findViewById(R.id.mainView);
Cursor c = myDB.doQuery("SELECT name, age from students");
while (c.moveToNext()){
TextView t = new TextView(this);
//t.setText("Some text");
//l.addView(t);
System.out.println(c.getString(c.getColumnIndex("name")) + "," + c.getLong(c.getColumnIndex("age")));
}
c.close();
}
public void doInsert(View v){
String[] vals = {"Steve","23"};
myDB.doUpdate("Insert into students(name, age) values (?,?);", vals);
}
public void doQuery(View v){
Cursor c = myDB.doQuery("SELECT name, age from students");
while (c.moveToNext()){
System.out.println(c.getString(c.getColumnIndex("name")) + "," + c.getLong(c.getColumnIndex("age")));
}
c.close();
}
}
答案 0 :(得分:0)
要从数据库中获取某些东西,请使用:
Cursor data
data = mydatabasereference.showData();//Show the board database
data.moveToFirst();//point the location to first instance of data
String text_data = (data.getString(1)));//Gets the string data
mytextbox.setText(text_data);
data.getstring(1)第二次进入数据库存储结构。请注意,它必须是字符串才能起作用。或者,您需要将该数据转换为字符串以进行打印。