Cannot add header view to list — setAdapter has already been called

后端 未结 10 2016
夕颜
夕颜 2020-12-03 13:38

I have one edittext field and one \"search\" button. When I click on search, I have to display a list view with data corresponding to the values entered in the edittext. I h

相关标签:
10条回答
  • 2020-12-03 14:16

    Cannot add header view to list -- setAdapter has already been called. which you can see, the myList.addHeaderView(header) must be execute before myList.setAdapter(adapter);

    0 讨论(0)
  • 2020-12-03 14:16

    I have used the following code in my sample application to set a ListView header:

    ListView lv = getListView();
    View headerView = getLayoutInflater().inflate(R.layout.layout_header, null, false);
    lv.addHeaderView(headerView);
    final TumblrDB db = new TumblrDB(this);
    c = db.query();
    startManagingCursor(c);
    adapter = new CustomCursorAdapter(this, R.layout.layout_del, c, new String[]{TumblrDB.DATE, TumblrDB.DESC}, new int[]{R.id.txt_a, R.id.txt_b});
    lv.setAdapter(adapter);
    
    0 讨论(0)
  • 2020-12-03 14:17

    The exception is thrown by android api.For API Level below KITKAT the addHeader() or the addFooter() method must be called before setAdapter() method.

    It is mentioned in the api documentation:

    Note: When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with Build.VERSION_CODES.KITKAT, this method may be called at any time. If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.

    0 讨论(0)
  • 2020-12-03 14:24

    Try this..

    dataAdapter = new MyCustomAdapter(this, R.layout.results_list_item, searchedResults);
    myList.addHeaderView(header);
    myList.setAdapter(dataAdapter);
    dataAdapter.notifyDataSetChanged();
    
    0 讨论(0)
  • 2020-12-03 14:24

    you can add FrameLayout as header view before setting adapter and dynamically add/remove view in FrameLaypout

    0 讨论(0)
  • 2020-12-03 14:30

    After I set

    final ViewGroup header = (ViewGroup) inflater.inflate(R.layout.item, listView, false);
    listView.addHeaderView(header, null, true); 
    

    before

    listView.setAdapter(adapter);
    

    a problem still appeared. Then I made Build > Clean Project.

    0 讨论(0)
提交回复
热议问题