简体   繁体   English

在Android中实现内部列表的最佳做法

[英]Best practices in realization of inner lists in android

I'm newbie in android developing so I'd like to ask for advice about solving the next problem. 我是android开发的新手,所以我想请教有关解决下一个问题的建议。

I have 2 SQlite tables: cards and meanings . 我有2个SQlite表格: cardsmeanings Each card has one or several meanings. 每张卡都有一个或多个含义。 My android application should show a list of the cards with the realted meanings. 我的Android应用程序应显示具有实际含义的卡片列表。 I should realise next operations: 我应该意识到下一个操作:

  • add a new card with a meaning 添加具有含义的新卡
  • add a meaning for an existing card 为现有卡添加含义
  • read card with meanings, update, delete 读取具有含义的卡,更新,删除

As far as I understand best way of realisation this is to make 2 content providers for the cards and for the meanings. 据我了解,最好的实现方式是为卡和含义创建2个内容提供程序。 Then I'd like to construct a class which would encapsulate cards' functionality and the put the cards into some kind of adapter. 然后,我想构造一个类,将封装卡的功能并将其放入某种适配器中。 And then bind the adapter to some kind of list view. 然后将适配器绑定到某种列表视图。

I'm not sure that this way is the most optimal, that's why any advices about better ways of doing this is highly appreciated. 我不确定这种方法是否是最佳方法,这就是为什么任何有关更好方法的建议都会受到赞赏的原因。

Since a card can have more one or more meanings, then you should use a third table, let's call it CardMeanings, which has two columns. 由于卡片可以具有一个或多个含义,因此您应该使用第三个表,我们将其称为CardMeanings,它具有两列。 The unique ID of a card, and the unique ID of a meaning. 卡的唯一ID和含义的唯一ID。 Each row of this table represents a unique combination of one card and one meaning. 该表的每一行代表一个卡片和一个含义的唯一组合。

Card      Meaning
  1          1
  1          3
  1          4
  2          7
  3          3
  3          4

Then use a single adapter to manage this table with a logical view of the relationship and low level methods, available only to the adapter, to maintain integrity. 然后使用单个适配器通过关系的逻辑视图和低级方法(仅对适配器可用)来管理该表,以维护完整性。 For example, if you delete a card, what should happen? 例如,如果您删除卡,应该怎么办? Normally, you would delete the card and all rows in the CardMeanings table with the given card ID but let a single adapter do that and manage all three tables. 通常,您将删除具有给定卡ID的卡以及CardMeanings表中的所有行,但让单个适配器执行此操作并管理所有三个表。

The key thing is that the whole point of the adapter is to turn a physical view (3 tables) into a logical view (cards and their meanings). 关键是适配器的重点是将物理视图(3个表)转换为逻辑视图(卡及其含义)。 Your activity code should only deal with the logical view and your adapter should be the only thing to deal with the physical view. 您的活动代码应仅处理逻辑视图,而适配器应是处理物理视图的唯一方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM