Android:sqlite 数据库存在基本问题

发布于 2024-10-31 13:21:33 字数 1521 浏览 0 评论 0原文

我在简单的 Android 应用程序中实现 sqlite 数据库时遇到一些麻烦:

在列表视图中向用户显示动物列表。选择动物后,用户将进入“动物”活动,该活动将显示动物的图片 全部的选项

  • 动物生物
  • 并为他们提供查看

,到目前为止非常简单,对吧?

我已经处理了数据库,它将填充动物的 listView。数据库目前看起来像

表动物- _ID, 姓名

表传记- _ID, 简介

在这里,我欢迎任何有关我的问题或如何改进我的实施的有用建议。

目前填充数据库如下,

long populateDB(){      


    String[] animalName = {"Lion" "Zebra", "Tiger", "Gorilla",...};
    String[] animalBios = {"Found in the "...}

    ContentValues animalNameVals = new ContentValues();
    ContentValues animalBioVals = new ContentValues();

    long[] rowIds = new long[animalName.length];


    // Populate the animal table
    for(int i = 0; i < animalName.length; i++){
            animalNameVals.put(KEY_ANIMALNAME, animalName[i]);
            rowIds[i] = db.insert(ANIMAL_TABLE, null, animalNameVals);
        }

    // Populate the Bio table
    for(int j = 0; j < bios.length; j++){
        animalBioVals.put(KEY_BIO, bios[j]);
        rowIds[j] = db.insert(BIOS_TABLE, null, animalBioVals);
    }

    return rowIds[0];
}

并计划能够通过传递带有意图的额外内容来告诉数据库选择了列表中的哪种动物,例如,如果 listItemClick 上的位置 == 1,则传递老虎并从数据库检索老虎生物。

问题:

然后在动物活动页面上是 getExtra() == Tiger,告诉活动从列表中选择了老虎并从数据库加载此生物..好吧,我看不到有效的方法实现这个想法的方法,并且我正在努力这样做。

我的第二个头痛来自于从 Db 将 Bio 添加到应用程序。最初,我在字符串中硬编码了一个测试 Bio,显示在 TextView 中。有没有办法从光标检索字符串并将其添加到 TextView id 中?我知道我需要一些适配器,我不明白的是为什么它不能像 setResource(R.id.bio) = bio 这样简单。

感谢您的阅读,非常感谢您的帮助。

I am having some trouble implementing a sqlite database in my simple android application:

a user is displayed a list of animals in a Listview.Upon selecting an animal the user is brought to an activity "Animal",which will display a picture of the animal and give them options to

  • view Animal Bio
  • Back

All very simple so far, right?

I have working the database, which will populate the listView of animals.Database currently looks like

Table Animal-
_ID,
Name

Table Biography-
_ID,
Bio

This is where I would welcome any helpful advice on my problem, or on how to improve my implementation.

Currently populating the DB as follows

long populateDB(){      


    String[] animalName = {"Lion" "Zebra", "Tiger", "Gorilla",...};
    String[] animalBios = {"Found in the "...}

    ContentValues animalNameVals = new ContentValues();
    ContentValues animalBioVals = new ContentValues();

    long[] rowIds = new long[animalName.length];


    // Populate the animal table
    for(int i = 0; i < animalName.length; i++){
            animalNameVals.put(KEY_ANIMALNAME, animalName[i]);
            rowIds[i] = db.insert(ANIMAL_TABLE, null, animalNameVals);
        }

    // Populate the Bio table
    for(int j = 0; j < bios.length; j++){
        animalBioVals.put(KEY_BIO, bios[j]);
        rowIds[j] = db.insert(BIOS_TABLE, null, animalBioVals);
    }

    return rowIds[0];
}

And had planned on being able to tell database which animal on list was selected by passing extras with the intent, eg if position on listItemClick == 1, pass in tiger and retrieve tiger bio from db.

Problems:

Then on the Animal activity page is getExtra() == tiger, telling the activity that tiger was selected from the list and to load this bio from the DB..well, I cannot see an efficient method of implementation for this idea and am struggling to do so.

My second headache comes from adding the bio to the application from the Db.Originally I had a test bio hardcoded in a string, shown in a TextView.Is there a way to retrieve a string from a cursor and add it to the TextView id?I understand I will need some adapter, what I do not understand is why cant it be as simple as setResource(R.id.bio) = bio.

Thanks you for reading and any help is much appriciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡Ms空城旧梦 2024-11-07 13:21:33

第一个问题:首先,我不确定为什么Animal表中没有Bio列?由于没有 Bio 适合除自身之外的任何其他动物,因此您可以安全地执行此操作。通过这样做,您可以在选择时查询数据库,并将整个对象(包括动物和生物的名称)传递到下一个活动,并使用它来获取您的信息。如果这有点不清楚,请告诉我,我会尽力解释得更好。

第二个问题:您可以使用游标从表(还有字符串)中获取值。要获取字符串,您可以执行类似的操作,其中 cursor 是带有数据库结果的光标:

 String bio;
// Move Cursor to its first element
if(cursor.moveToFirst()) {
     // Make sure the cursor is not null
    if(cursor != null) {
        bio = cursor.getString(cursor.getColumnIndex("Bio")));
    }
}

旁注:如果我正确地阅读了代码,则似乎您使用ID 的?据我所知,ID 通常是整数。

First problem: First of all, I'm not sure why you don't have the column Bio in the Animal-table? As no Bio would fit to any other animal than itself, you can safely do this. By doing this you can query the database upon selection and pass the entire object (including name of animal and bio) to the next Activity and use this to get your information. If this was somewhat unclear, let me know and I'll try to explain it better.

Second problem: You can get values from tables (there of also Strings) using a Cursor. To get the String you can do something like this where cursor is the Cursor with your result from the database:

 String bio;
// Move Cursor to its first element
if(cursor.moveToFirst()) {
     // Make sure the cursor is not null
    if(cursor != null) {
        bio = cursor.getString(cursor.getColumnIndex("Bio")));
    }
}

Sidenote: If I read the code correctly, it seems that you use long for ID's? The usual thing to go about ID's is integers as far as I know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文