単一のアダプタで2つの視聴者を使用する方法
-
23-12-2019 - |
質問
ListViewのセクショニングを使用している間に問題に直面しています。私はListViewスクロールをスムーズにするためにビューホールダーを使用する必要がありますが、2つの視聴者が2つの別のビューがあるとはわかりません。私が間違っているところで教えてください。
コード
@
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;
}
.
エラー
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)
. 解決
getItemViewType()
にListAdapter
をオーバーライドする必要があり、行の種類ごとに異なる番号を返す必要があります。あなたの場合は、セクションと0
for通常の行を返すことができます。その後、1
がリサイクル用の適切なgetView()
型で呼び出されます。
@Override
public int getItemViewType(int position) {
if (getItem(position).isSection()) {
return(0);
}
return(1);
}
. 所属していません StackOverflow