Domanda

I'm trying to give the users of my app the option to change how they want their results displayed.

I've created a different layout item for each view and extended from BaseAdapter like so:

public View getView(int index, View recycledCompatibleView, ViewGroup parent) 
{
    // Just in case the view can be reused
    View toReturn = recycledCompatibleView;
    if(toReturn == null)
    {           
        LayoutInflater inflator = LayoutInflater.from(parent.getContext());
        toReturn = inflator.inflate(layoutId, null);
    }
    ...
}

public void setResultsListStyle(int layoutId)
{
    this.layoutId = layoutId;
}

Calling notifyDataSetChanged() is (observable through debug) refreshing the view because the new view is being inflated and returned from getView() method.

However the view on screen is not changing...

Is there something I'm missing ?

È stato utile?

Soluzione

The problem here is that ListView may cache already inflated Views that are of old "format". You need to somehow reset this View "cache". For this you can use next trick:

mListView.setAdapter(mListView.getAdapter());

Instead of calling notifyDataSetChanged().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top