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...).

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP