3

我正在使用以下代码

ConsumersFiltersAdapter genderFilterAdapter, ageFilterAdapter,
            lastCheckinFilterAdapter, customerTypeAdapter;  

            genderFilterListView.setAdapter(genderFilterAdapter);
            ageFilterListView.setAdapter(ageFilterAdapter);
            lastCheckinFilterListView.setAdapter(lastCheckinFilterAdapter);
            customerFilterListView.setAdapter(customerTypeAdapter);

            genderFilterListView.setOnItemClickListener(this);
            ageFilterListView.setOnItemClickListener(this);
            lastCheckinFilterListView.setOnItemClickListener(this);
            customerFilterListView.setOnItemClickListener(this);

然后我实现以下功能

@Override
    public void onItemClick(final AdapterView<?> listView, final View view,final int position, final long id) {
        ConsumersFiltersAdapter consumersFiltersAdapter=(ConsumersFiltersAdapter)listView.getAdapter();
        consumersFiltersAdapter.setSelectedIndex(position);
    }

但我在第一行得到一个类转换异常

java.lang.ClassCastException: android.widget.HeaderViewListAdapter cannot be cast to com.abc.merchant.adapter.ConsumersFiltersAdapter
4

1 回答 1

2
 convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                   setSelectedIndex(position);
                }
          });

您可以为此使用上面的代码:两个代码的工作方式相似。

@Override
    public void onItemClick(final AdapterView<?> listView, final View view,final int position, final long id) {
        ConsumersFiltersAdapter consumersFiltersAdapter=(ConsumersFiltersAdapter)listView.getAdapter();
        consumersFiltersAdapter.setSelectedIndex(position);
    }
于 2013-05-29T10:30:00.617 回答