Android 初学者:用于简单视图或直接数据库查询的适配器?

发布于 2024-09-17 20:50:53 字数 566 浏览 6 评论 0原文

首先,我对 Android 还很陌生(不到 4 天)。

我正在尝试了解数据库数据如何链接到视图和小部件。

我已经完成了一些教程,我注意到适配器用于链接 AdapterView,这些视图(据我所知)包含一堆相同的子视图(例如列表、图库等)。因此适配器负责创建这些子视图并为每个子视图填充数据(如果我错了,请纠正我)。

现在假设我有一个列表视图,其中列出了例如酒店。列表中的每一行都有酒店名称和基本评级(例如 5 星级)。现在,当您点击列表中的酒店时,会显示一个新活动,显示该特定酒店的详细信息。所有数据都在数据库中。我知道您有一个适配器管理列表的数据<->视图链接,但是管理酒店详细信息视图数据的最佳方法是什么(这不是列表,而只是几个文本视图和图像)例如)?

最好只在意图中传递 ID,然后让详细信息活动自行从数据库获取数据(在这种情况下,我是否将查询存储在详细信息活动中?)?或者您是否获取所需的所有字段并将其直接放入意图中?您是否需要一个针对实际上不会生成大量类似子视图的视图的适配器?

我想我的问题的总结是,当您不处理适配器视图而只是简单直接的单个视图时,您会使用什么来代替适配器。

感谢您提供的任何帮助。

First off I'm really new to android (< 4 days).

I'm trying to wrap my head around how database data is linked to Views and widgets.

I've done a few tutorials and I've noticed that Adapters are used to link AdapterViews which are (as I understand it) Views which contain a bunch of identical subviews (eg lists, gallery, etc). So the Adapter is responsible for creating those subviews and populating the data for each one (correct me if I'm wrong).

Now let's say I have a list view which lists Hotels for example. Each row in the list has the Hotel's name and a basic rating (eg 5 star). Now when you tap on a hotel in the list a new activity shows up showing the details of that particular hotel. All the data is in a database. I understand that you have an adapter manage the data<->view link for the list, but what's the best way to then manage data for the hotel details view (which is not a list but just a couple of text views and an image for example)?

Is it best to just pass the ID in the intent and then have the details activity fetch the data from the DB on its own (in this case do I store the query in the details activity?)? Or do you get all the fields you need and put those in the intent directly? Do you need an adapter for a view which doesn't actually generate lots of similar subviews?

I guess a summary of my question is what do you use instead of an adapter when you're not dealing with adapterviews but just simple straightforward single views.

Thanks for any help you can provide.

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

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

发布评论

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

评论(2

弥繁 2024-09-24 20:50:53

最好只在意图中传递 ID,然后让详细信息活动从数据库中获取数据
单独(在这种情况下,我是否将查询存储在详细信息活动中?)?

这就是我的建议。

对于实际上不会生成大量类似子视图的视图,您是否需要一个适配器?

不可以。您只能将 AdapterAdapterViewListViewSpinner 等)结合使用。

我想我的问题的总结是,当您不处理时,您会使用什么而不是适配器
适配器视图,但只是简单直接的单一视图。

只是数据库中的Cursor。从 Cursor 中的(一行)获取字段,将它们放入 EditText 小部件中,当用户进行更改时,更新该行。

Is it best to just pass the ID in the intent and then have the details activity fetch the data from the DB
on its own (in this case do I store the query in the details activity?)?

That is what I would recommend.

Do you need an adapter for a view which doesn't actually generate lots of similar subviews?

No. You only use an Adapter with an AdapterView (ListView, Spinner, etc.)

I guess a summary of my question is what do you use instead of an adapter when you're not dealing with
adapterviews but just simple straightforward single views.

Just the Cursor from the database. Get the fields from the (one) row in the Cursor, put them in the EditText widgets, and when the user makes changes, update the row.

独夜无伴 2024-09-24 20:50:53

@CommonsWare 很火爆。作为一个代码示例,我能够通过创建一个辅助方法来动态设置 TextViews 来使事情变得更简洁。

在我的 onCreate() 中,我有以下几行:

bindTextView(hotel, "uid");   // `hotel` is the Hotel object with attributes.

然后我在下面定义 bindTextView()如下所示:

protected void bindTextView( Hotel hotel, String attribute ) {

    try {

        // Get field for object dynamically.
        Field field = hotel.getClass().getField(attribute); 

        // Invoke field "getter" method to get value.
        String value = field.get(hotel).toString();                                                                   

        // Get resource id dynamically.
        int resourceId = R.id.class.getField(attribute).getInt(null);                                               

        // Get element with resource id.
        TextView element = (TextView) this.findViewById( resourceId );                                              

        // Finally, set the element's text value.
        element.setText( value );

    } catch (IllegalAccessException e) { e.printStackTrace();                                                       

    } catch (NoSuchFieldException e)   { e.printStackTrace(); }                                                     

}

我实际上将此帮助器方法移至基 Activity 类,因为它在许多不同的活动之间共享。

我希望这可以帮助一些人保持活动的整洁。

太平绅士

@CommonsWare is bang on. As a code example, I was able to DRY things up by creating a helper method to dynamically set the TextViews a little cleaner.

In my onCreate() I have a number of the following lines:

bindTextView(hotel, "uid");   // `hotel` is the Hotel object with attributes.

And then I define bindTextView() below as follows:

protected void bindTextView( Hotel hotel, String attribute ) {

    try {

        // Get field for object dynamically.
        Field field = hotel.getClass().getField(attribute); 

        // Invoke field "getter" method to get value.
        String value = field.get(hotel).toString();                                                                   

        // Get resource id dynamically.
        int resourceId = R.id.class.getField(attribute).getInt(null);                                               

        // Get element with resource id.
        TextView element = (TextView) this.findViewById( resourceId );                                              

        // Finally, set the element's text value.
        element.setText( value );

    } catch (IllegalAccessException e) { e.printStackTrace();                                                       

    } catch (NoSuchFieldException e)   { e.printStackTrace(); }                                                     

}

I actually moved this helper method to a base Activity class since it's shared amongst a number of different activities.

I hope that helps some people keep their activities clean.

JP

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