gpt4 book ai didi

java - 在 TextView Android studio 上显示数据库中的信息

转载 作者:行者123 更新时间:2023-12-01 16:45:13 26 4
gpt4 key购买 nike

我需要帮助,我正在尝试在 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();
}


}

最佳答案

要从数据库中获取某些内容,请使用:

   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) 第二次获取数据库存储结构内部。请注意,它必须是一个字符串才能起作用。或者您需要将该数据转换为字符串才能打印。

关于java - 在 TextView Android studio 上显示数据库中的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61793411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com