【问题标题】:ListView Row Containing Multiple Dynamic Spinners包含多个动态微调器的 ListView 行
【发布时间】:2019-03-08 13:32:40
【问题描述】:

我在特定行的 listview 控件中有多个 Spinner。 我从 WebService 动态填充所有微调器,所有微调器值都可以在所选项目上更改。

经验。我有 3 个国家和州和城市的微调器,然后所有这 3 个微调器从 WS 填充,而用户选择国家,然后在 countryselecteditem 上更改州和城市的所有数据。并在 State selected item city 数据更改...

当时我滚动并继续该行 getView() 调用所有 3 个选定项并按选定项填充所有适配器。

如何处理列表视图中的 3 个微调器?

【问题讨论】:

  • 国家、州、城市微调器是在 listView 的同一行还是不同行?

标签: android android-listview android-spinner


【解决方案1】:
  1. 设计一个包含 3 个您希望的微调器的 ListItem 布局,然后将其设置为 ListView 适配器。
  2. 然后使用一个包含 3 个 ID(如果需要更多)的 POJO 类的通用类型的 SubClassArrayAdapter,如下所示,并将 Adapter 设置为 ListView。

    class SpinnersStateInListItem
    {
      private int countrySelectedPosition=0;
      private int stateSelectedPosition=0;
      private int citySelectedPosition=0;
      //here you need to generate respective getters and setter methods.
     }
    

    //执行以下代码将 SubClassArrayAdapter 设置为您从自定义视图的 ArrayAdapter 类派生的列表视图

      SubClassArrayAdapter<SpinnersStateInListItem> = new SubClassArrayAdapter<SpinnersStateInListItem>(context,resourceid,listof SpinnersStateInListItem Generic type);
    

    在您的 SubClassArrayAdapter 类的 getView() 中,创建 3 个微调器对象并将数组适配器设置为第一个微调器,然后将 OnItemSelectedListeners 设置为 3 个微调器。 然后在第一个 Spinner OnItemSelected 方法中将 ArrayAdapter 设置为 2nd Spinner,它将包含根据在第一个 Spinner 中选择的 Country 的 State 列表。然后在 2nd Spinners OnItemSelected 方法中将数组 Adapter 设置为 3rd Spinner,该方法包含根据所选国家和所选州的城市列表。在这里,您应该根据所选项目过滤数据。 When Items are selected immediately save them to SpinnersStateInListItem respective object so that even if you reload the listview all the items state will remain as it is.例如如下。

    OnItemSelected()
    {
     if(v==firstspinner)
      {
        listofSpinnersStateinListItem.get(listitemposition).setCountrySelectedPosition(spinnerselectposition);  
       } 
      }
    

对于州和城市也是如此。

  1. 最初,您获取第一个国家的数据并将其设置为 First Spinner,它同时获取第一个国家的州,然后是第一个国家,第一个州相关的城市。

希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    相关资源
    最近更新 更多