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 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题