I am facing an issue while using sectioning in a listview . I need to use viewholder to make listview scroll smooth but I don't know how to implement two viewholders as ther are two seperate views one is section and second is simple entry .Here is the code what I have tried and the error I got .Please tell me where I am doing wrong and how can I correct it :
CODE
@
Override
public View getView(int position, View convertView, ViewGroup parent) {
final GroupAccountdto i = items.get(position);
if (i != null) {
if (i.isSection()) {
if (convertView == null) {
convertView = vi.inflate(R.layout.list_group_section, null);
ViewHolderGroup group_holder = new ViewHolderGroup();
group_holder.group_name = (TextView) convertView.findViewById(R.id.list_section_group_name);
convertView.setTag(group_holder);
}
ViewHolderGroup group_holder = (ViewHolderGroup) convertView.getTag();
GiddhGroups si = (GiddhGroups) i;
group_holder.group_name.setText(si.getGroupname());
} else {
if (convertView == null) {
convertView = vi.inflate(R.layout.list_item_account, null);
ViewHolderAccount account_holder = new ViewHolderAccount();
account_holder.account_name = (TextView) convertView.findViewById(R.id.list_item_account_name);
account_holder.account_bal = (TextView) convertView.findViewById(R.id.list_item_account_balance);
}
ViewHolderAccount account_holder = (ViewHolderAccount) convertView.getTag();
GiddhAccounts ei = (GiddhAccounts) i;
if (account_holder.account_name != null)
account_holder.account_name.setText(ei.getAccountName());
if (account_holder.account_bal != null)
account_holder.account_bal.setText(ei.getBalance());
}
}
return convertView;
}
static class ViewHolderGroup {
TextView group_name;
}
static class ViewHolderAccount {
public TextView account_name;
public TextView account_bal;
}
ERROR
02-27 12:49:28.461: E/AndroidRuntime(20200): java.lang.ClassCastException: adapters.GroupAccountAdapter$ViewHolderGroup cannot be cast to adapters.GroupAccountAdapter$ViewHolderAccount
02-27 12:49:28.461: E/AndroidRuntime(20200): at adapters.GroupAccountAdapter.getView(GroupAccountAdapter.java:53)
02-27 12:49:28.461: E/AndroidRuntime(20200): at android.widget.AbsListView.obtainView(AbsListView.java:2334)
02-27 12:49:28.461: E/AndroidRuntime(20200): at android.widget.ListView.measureHeightOfChildren(ListView.java:1409)
02-27 12:49:28.461: E/AndroidRuntime(20200): at android.widget.ListView.onMeasure(ListView.java:1273)
You need to override getItemViewType()
in your ListAdapter
, returning a distinct number for each type of row. In your case, you could return 0
for sections and 1
for regular rows. Then, getView()
will be called with the proper View
type for recycling.
@Override
public int getItemViewType(int position) {
if (getItem(position).isSection()) {
return(0);
}
return(1);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.