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

后端 未结 10 2017
夕颜
夕颜 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:33

    If you were used android:entries in ListView in xml file, Its called setAdapter() method before addHeaderView. So remove android:entriesattribute from ListView in xml layout file. It will be work.

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

    On android 2.3, add header after setAdapter (even if you have added early, then removed) will throw an exception. To hide or show a header dynamically, use setVisibility(). How? You can see Hiding header views.

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

    I had the same problem today. I have multiple ListViews.. With the information from the first, it builds the list of the next one and everyone has setAdapter in it. For me, the best solution was to put

    setListAdapter(null);
    

    on top of the function, where I inflate the Header. I hope this helps..

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

    After so much efforts i got solution for my side i hope this will helps someone too

    i already set adpater at the last (after view added) but dont know why i was suffring from same error so here i did something like this code

    // Set View here
    View view = getLayoutInflater().inflate(R.layout.navigation_header,null);
    mDrawerList.addHeaderView(view);
    // init your adapter
    adapter1 = new YourListAdapter(getApplicationContext(),blabla);
    // set adapter into handler
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {        
       @Override
      public void run() {
      // TODO Auto-generated method stub
      mDrawerList.setAdapter(adapter1);
      }
    }, 100);
    

    I put my adapter in to handler sometime it happends that adapter set faster then view so this code enough for me to solve this exception. :)

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