I’m actually creating a ListView by populating items from the database.
When the end of the listview is reached I populate a few more items. Now, I want the dividers of the ListView based on the value returned from the database. If two consecutive values of the database are same I want them to be seperated by a thinline and if not a thick line.
I tried setting them through the adapter like this
if (convertView == null) {
holder = new ViewHolder();
if (eventSource.get(position).equalsIgnoreCase("asdsadas")
&& eventSource.get(position + 1).equalsIgnoreCase(
"fdgdfgfd")
|| eventSource.get(position).equalsIgnoreCase(
"dfgdfgdfg")
&& eventSource.get(position + 1).equalsIgnoreCase(
"jgghjhhgg")) {
convertView = mInflater.inflate(R.layout.list_adapter, null);
} else {
convertView = mInflater.inflate(R.layout.list_adapterthinline,
null);
}
I’m inflating a new layout based on the condition. It works for the first time, but after I scroll down and come up the view changes. It gets all mixed up.
I tried setting the divider height in the Activity too, like this and I call the ‘setdivider’ method in onCreate and in onScroll listener too.
public void setdivider() {
// TODO Auto-generated method stub
for (int i = 0; i < listSource.size() - 1; i++) {
if (!listSource.get(i).equalsIgnoreCase(
listSource.get(i + 1))) {
Log.v("inside not equals", "become smalllllllllllllllll");
list.setDivider(red);
list.setDividerHeight(5);
} else if (listSource.get(i).equalsIgnoreCase(
listSource.get(i + 1))) {
Log.v("inside equals", "become bigggggggggggg");
list.setDivider(blue);
list.setDividerHeight(10);
}
}
}
But here even though both the log comments are shown on the LogCat, only one divider is set in the list.
Please tell me where I’m going wrong, or do suggest some other approaches, if any.
ListViewcaches views, so when you scroll around, view are reused. This is whyconvertViewisn’t always null. However, since you have two different kinds of views, you need to tell that to theListViewso that theconvertViewyou get back is the kind you want. You do that by implementingAdapter.getItemViewType()andAdapter.getViewTypeCount().In your example, you would let
getViewTypeCountreturn 2, and letgetItemViewTypereturn 1 if it’s a divider, and 0 if it’s not.