0

I have a listview and in its adapter according to some situations I inflate layouts. for example consider this:

if (state1){
    resource = R.layout.layout_1

}else if (state2){
    resource = R.layout.layout_2
}
if (convertedView == null) {
    view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(resource, parent,false);
}               
return view;

But sometimes ListView gets confused and it shows same layout for different states. I am sure there nothing wrong in code. But in ListView behavior. Any Suggestion? Thanks

user2808671
  • 271
  • 2
  • 13
  • How do you get the state? Is the state per row or per Listview? – hoomi Aug 30 '14 at 07:40
  • I get the state according to the new added data :) But this is not the problem. Please look at my own answer :) – user2808671 Aug 30 '14 at 07:49
  • [Did you know `ListView` use recycling mechanism?](http://stackoverflow.com/a/14108676/1841194) – frogatto Aug 30 '14 at 10:01
  • Yea but what I experienced was something other than recycling I think! Because it doesn't wait for old items to go out of the list(By Scrolling) – user2808671 Aug 30 '14 at 10:04

2 Answers2

0
if (state1){
view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.layout_1, parent,false);

}else if (state2){
view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.layout_2, parent,false);
}




return view;
Dakshesh Khatri
  • 639
  • 7
  • 12
0

Well I found the Solution.

The problem is that I check if ConverView parameter is NOT null then the adapter doesn't inflate new view and it uses the last one. If I delete this condition and make the adapter to always inflate a new layout the problem gets solved.

Like this:

public View getView(int position, View convertedView, ViewGroup parent) {
View view;
int resource = 0;
if (state1){
    resource = R.layout.layout_1;
}else{
    resource = R.layout.layout_2;

}
//if (convertedView == null) { //I should Delete this condition
     view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.layout_2, parent,false);
//}
return view;
user2808671
  • 271
  • 2
  • 13