0

I have a row in a listview that I need to change its layout dynamically. In my array adapter I have the following (simplified) code

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView == null) {
        convertView = inflater.inflate(R.layout.default);
    }
    ...

    if(condition) {
        View view = inflater.inflate(R.layout.new);
        ...
        return view; // doesn't work
    }
    return convertView;
}

My actual listview is actually pretty complex and I am also using the getViewType for the two different layouts. But the above code should be enough to make my point that the new layout won't display correctly when I want to switch layout dynamically.

My question is, how can I refresh the convertView, or trigger a reload of the adapter/listview to the point that the convertViews will be cleared? I've tried calling notifyDataSetChanged, or calling the listview invalidateViews() but they all didn't worked. My last resort seems to restart the whole activity but I don't think this is an elegant solution.

Neoh
  • 15,906
  • 14
  • 66
  • 78
  • Why not use getViewType with additional view type (it's possible change view type on some position dinamically)? – sandrstar Jul 20 '13 at 09:56
  • This could be because you don't have an `else` block here. Try adding the `else` code: perhaps, just `return convertView;`. – Vikram Jul 20 '13 at 10:00

0 Answers0