dongzg2006 2015-08-04 23:04
浏览 48
已采纳

Android数组到mysql数据库?

I am having a seperate table of user interests. Then, for instance, my users table:

| uID | uName |
| 1   | Brad  |
| 2   | Jake  |

My interest table

| iID | iInterest |
| 1   | Cars      |
| 2   | Computers |

I have a many to many relationship table that links the users to the interests (UserInterests):

| uID | iID |
| 1   | 1   |
| 1   | 2   |
| 2   | 2   |

In my android app, I have an activity which contains checkboxes for each of the interest. Users can also choose multiple interest. For example Car, Computer, Technology etc. Whatever the user has selected will be stored in java userInterest arrayList. Now, how can I add that userInterest array to mysql. I really need some help!

  • 写回答

1条回答 默认 最新

  • douke1954 2015-08-04 23:27
    关注

    If this were my project i would keep track of id's the user clicked on inside of an ArrayList. Set a listener on each checkbox that either add's or removes the Long object from the Arraylist. When the user is done, you can either iterate throw the ArrayList, calling insert on your database for each Long to add the iID.

    An alternative would be to keep track of clicks with a SparseBooleanArray, this way you can keep track of position and if its "on" - True or "Off" False.

    Another really cool way of doing this would be to implement the Command Design Pattern. You could store individual Insert/Delete commands inside your invoker object then call execute on each Command object.

    Edit: The more I think about it, the more the command pattern would be really cool for this situation. It would also take care of any deletion as well as insertion if you implement it correctly. If the lists are huge you could run into performance problems doing individual inserts per row. You might also want to investigate a batch insert command (blanking on the SQL terminology...).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统
  • ¥15 快手联盟怎么快速的跑出建立模型