【问题标题】:add and remove a header of this listView添加和删​​除此 listView 的标题
【发布时间】:2013-06-09 07:57:47
【问题描述】:

我有一个列表视图。 listView 的内容存储在适配器中维护的列表中。我想在单击按钮时添加和删除此 listView 的标题。当我添加标题时,我得到适配器已经设置的异常。还有其他方法吗?请帮助。

【问题讨论】:

    标签: android listview


    【解决方案1】:

    在 ListView 上,您必须在 .setAdapter() 之前调用 .addHeader().addFooter()。我的建议是在需要时modify your adapter to display a header row as a first row,而不是使用 .addHeader()。您可能必须在适配器中为此添加新的行类型。此外,为您的适配器添加方法,使您能够隐藏/显示此标题行,但不要忘记在此之后调用 .notifyDatasetChanged()

    【讨论】:

      【解决方案2】:

      添加Header后必须设置适配器,像这样

      View header = getLayoutInflater().inflate(R.layout.header, null);
      ListView listView = getListView();
      listView.addHeaderView(header);
      setListAdapter(new ArrayAdapter<String>(this,
                      android.R.layout.simple_list_item_single_choice,
                      android.R.id.text1, names));
      

      【讨论】:

        【解决方案3】:

        只需先在 listview 上设置页眉和页脚,然后设置适配器。

        喜欢,(记住我只是建议你程序,方法可能与我在这里写的不同。)

         // stuff of header
         listview.addHeaderView(View v);
        
         //stuff of footer
         listview.addFooterView(View v);
        
         //stuff of adapter
         listview.setAdapter(mAdapter);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-01-05
          • 2018-08-19
          • 2015-04-01
          • 1970-01-01
          • 2013-08-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多