动态创建视图并将其映射到对象

时间:2015-05-25 17:07:57

标签: java android android-custom-view android-recyclerview

我创建了以下活动:

enter image description here

它会滚动,如果你滚动,你会看到这个

enter image description here

所以你有两个房间:第一个是双人房,有两个乘客(paxes)。第二个房间是单人房,只有一名乘客。

现在,我的活动需要动态创建,以适应用户选择预订的房间组合。它可能是两个单人房,两个双人房等等......

我在此活动中需要做的是让用户插入每位乘客的姓名和姓氏,然后将其发送给网络服务以完成预订。

我已经为乘客准备了一个物品。它是Pax类,有三个字段:

  • String title
  • String name
  • String surname

你可以猜到它的用途。

现在,我还创建了一些RoomInstance对象,每个RoomInstance都有以下参数:

  • String type - >单,双
  • int numberOfPaxes - >这个房间有多少人
  • ArrayList<Pax> paxes - &gt;乘客名单

我通过在搜索时创建的RoomInstances列表上循环来创建上面的活动。这就是我的工作:

        for(LatestSearchData.RoomInstance ri : latestSearchData.getRooms()) {
        /**
         * Sets up a header for each room in our list
         */
        LayoutInflater inflater = LayoutInflater.from(PaxSelectorActivity.this);
        View inflatedHeader = inflater.inflate(R.layout.activity_pax_selector_header, null, false);
        TextView headerText = (TextView) inflatedHeader.findViewById(R.id.pax_selector_header);
        headerText.setText(ri.getType());
        hostingLayout.addView(inflatedHeader);

        for(int i = 0; i < ri.getNumberOfPaxes(); i++) {
            /**
             * Sets up a layout to fill up for each pax
             */
            View inflatedLayout = inflater.inflate(R.layout.activity_pax_selector_single_room, null, false);
            EditText nameEditText = (EditText) inflatedLayout.findViewById(R.id.name);
            EditText surnnameEditText = (EditText) inflatedLayout.findViewById(R.id.surname);
            (...)
            hostingLayout.addView(inflatedLayout);

我为每个房间膨胀一个标题(屏幕截图中标有DOUBLE和SINGLE的标题),然后为每个乘客填充一个activity_pax_selector_single_room行。它优雅地工作,我得到了我想要的确切活动。但是......我如何映射插入房间和乘客的数据?我想链接它们,以便一旦用户点击“下一步”并将正确的乘客放在正确的房间,就能够轻松地检索它们,因为这是一个有价值的信息。

我正在考虑扩展LinearLayout并使用RoomInstance参数创建自定义视图,然后将RoomInstance传递给布局。但在我看来,这是一个巨大的MVC逻辑突破,而我宁愿不这样做。此外,由于房间列表不会太长,我将在乘客之间有任意数量的标题,我认为使用RecyclerView不是一个聪明的主意......或者是它?

0 个答案:

没有答案