【问题标题】:How to retrieve and display data from a database to TextView如何从数据库中检索数据并将其显示到 TextView
【发布时间】:2018-02-26 17:37:53
【问题描述】:

我已经为我的数据库和 TextView 构建了代码,但是 (EDITED:) 我不知道 STEPS 如何从我的数据库中检索数据并显示它们在 TextView 中。 一般来说,为了拍照,我会使用数据库中的地图保存另一个活动的位置。这是我的数据库代码:

 class MyDatabaseHandler extends SQLiteOpenHelper {

 private static final int DATABASE_VERSION = 1;
 private static final String DATABASE_NAME = "MapLocations.db";
 private static final String TABLE_LOCATIONS = "Locations";
 private static final String COLUMN_ID = "_id";
 private static final String COLUMN_LOCATIONNAME = "LocationName";

 MyDatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
 }

 @Override
 public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE " + TABLE_LOCATIONS + "(" + COLUMN_ID +
            " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_LOCATIONNAME +
            " TEXT" + ");";
    db.execSQL(query);
 }

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE " + TABLE_LOCATIONS);
    onCreate(db);
 }

 void addLocation(String string) {
    ContentValues values = new ContentValues();
    values.put(COLUMN_LOCATIONNAME, string);
    getWritableDatabase().insert(TABLE_LOCATIONS, null, values);
 }
}

这是我在 TextView 中的活动:

   public class PreviousLocationsActivity extends AppCompatActivity {

TextView textv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_previous_locations);
    textv = (TextView) findViewById(R.id.dbtextv);
    (????)
}

我知道这很简单,但我需要一点帮助。 我只想检索显示我的数据库在 TextView 中可能拥有的任何数据。 我怎样才能做到这一点?

【问题讨论】:

  • 您对哪一部分有问题?您是否在查询数据库、将响应转换为文本或设置 textview 的文本时遇到问题?
  • this solution 可能会有帮助
  • 错误是你要代码,这意味着你在做你的工作而不是你。只有您应该编写代码。我们可以帮助您编写它,但不能代替您编写。因此,如果您需要帮助,请向Take data from my database 说明具体是什么问题?为什么你做不到?
  • 我需要步骤对不起步骤或程序来做这个代码是好的。
  • 你的问题有问题的是你要求完整的代码,并且似乎拒绝阅读如何自己解决这个问题的教程。

标签: android sqlite textview


【解决方案1】:

我只想在 TextView 中显示我的数据库可能包含的任何数据,以显示您可以使用 textv.setText("Your data will be here"); 用于您的数据库的数据,您可以看到此引用链接 Display data from database into textview

【讨论】:

  • 是的,但是如何从我的数据库中获取数据?
  • 先打开数据库,取数据,关闭数据库通过这个链接Click Me取数据Click Here
【解决方案2】:

首先从数据库中获取数据。

然后是 textv.setText("text");

请检查链接。 https://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多