【问题标题】:Add items to adapter in Xamarin.Android在 Xamarin.Android 中将项目添加到适配器
【发布时间】:2018-05-11 07:46:08
【问题描述】:

我正在 Xamarin.Android 中开发一个应用程序。在这里,我使用带有 baseadapter 的 listview 来附加联系人列表。现在,一旦从数据库滚动到列表视图的末尾,我需要追加项目。我的意思是这个概念就像“加载更多项目”。起初,我通过在滚动到列表视图末尾时重新创建它来将项目添加到适配器。但我从链接 Load More Item in lazy adapter and listview 了解到这不是最佳方式。 我的代码如下,

allContactsList -> 从数据库中获取项目

适配器 -> 我的自定义适配器

if (allContactsList != null && allContactsList.Count() > 0)
                    {
                        if(adapter == null)
                            adapter = new AllContactsListAdapter(this, allContactsList);
                        else
                        {
                           //Stucks here. I can't append to adapter
                        }

                        adapter.NotifyDataSetChanged();
                        listView.Adapter = adapter;
                    }

如何将新项目附加到 Xamarin.Android 中的适配器?

【问题讨论】:

    标签: android listview xamarin


    【解决方案1】:

    在您的Adapter 中添加一个方法,如下所示:

    public void addItem(List<string> list)
    {
        mList.AddRange(list);
        NotifyDataSetChanged();
    }
    

    我在github上提供了一个演示。

    【讨论】:

    • 很高兴它有帮助,您能接受它作为答案吗?
    • 一个小疑问。是否有必要在适配器中添加方法'public void addItem(List list)'?实际上,我确实在 Activity 本身中添加了项目代码。它也有效。
    • 我是第一个回答你问题的人。那你转过Recyclerview了吗?
    • 使用ListView还是RecyclerView,看需要。在这里,我使用了 Listview,因为我只需要列出联系人。就这样。参考这个,stackoverflow.com/questions/29957490/…
    【解决方案2】:

    Listview 已被弃用,不再在专业应用程序中使用。请改用支持库中的 Recyclerview。它在加载项目时更加优化并且具有更多功能。加载项的结构几乎相同。当您想将项目添加到现有列表中时,只需将它们添加到您的内容列表中,然后调用NotifyDataSetChanged();,正如@Joe Lv - MSFT 所说。

    【讨论】:

      【解决方案3】:

      我的工作代码如下。

      List<MyClass> existingAllContactsList;
      MyAdapter adapter;
      ListView listView;
      
      protected override void OnCreate(Bundle savedInstanceState)
      {
      ...
      ...
      BindData();
      }
      
          void BindData()
          {
      
          var allContactList = list of items // from database
      
          if (allContactList != null && allContactList .Count() > 0)
                              {
                                  if (adapter == null)
                                  {
                                      existingAllContactsList= allContactList;
                                      adapter = new MyAdapter(this, existingAllContactsList);
                                      listView.Adapter = adapter;
                                  }
                                  else
                                  {
                                      if (existingAllContactsList!= null && existingAllContactsList.Count() > 0)
                                          existingAllContactsList.AddRange(allContactList);
                                      else
                                          existingAllContactsList= allContactList;
                                  }
      
                                  adapter.NotifyDataSetChanged();
                              }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多