转换视图和多个类型BaseAdapter

时间:2014-02-21 13:13:09

标签: android

我创建了一个提供列表视图的适配器。它工作正常,但有点jenky所以我试图使用转换视图,以避免在每个getView调用上膨胀视图。我将getview方法更改为以下

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Suggestion suggestion = getItem(position);
    int type = getItemViewType(position);
    ImageView imageView;
    TextView titleTv;
    ImageView checkBox;
    switch (type) {
        case Suggestion.CONTACT_SUGGESTION_ID:
            ContactSuggestion contactSuggestion = (ContactSuggestion) suggestion;
            String id = contactSuggestion.getId();
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.contact_history_item, parent, false);
            }
            imageView = (ImageView) convertView.findViewById(R.id.imageView);
            PhotoRetreiver photoRetreiver = (PhotoRetreiver) imageView.getTag();
            if (photoRetreiver != null) {
                photoRetreiver.cancel(true);
            }
            if (images.containsKey(id)) {
                imageView.setImageBitmap(images.get(id));
            } else {
                photoRetreiver = new PhotoRetreiver(context, id, imageView);
                imageView.setTag(photoRetreiver);
                photoRetreiver.execute();
            }
            titleTv = (TextView) convertView.findViewById(R.id.titleTv);
            titleTv.setText(suggestion.getTitle());
            checkBox = (ImageView) convertView.findViewById(R.id.favouriteIv);
            checkBox.setOnClickListener(this);
            checkBox.setId(position);
            if (suggestion.isFavourite()) {
                checkBox.setBackgroundResource(R.drawable.search_fav_active);
                checkBox.setTag(true);
            } else {
                checkBox.setTag(false);
            }
            break;
        case Suggestion.GOOGLE_SUGGESTION_ID:
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.google_history_item, parent, false);
            }
            imageView = (ImageView) convertView.findViewById(R.id.imageView);
            titleTv = (TextView) convertView.findViewById(R.id.titleTv);
            imageView.setImageResource(resources.getIdentifier(suggestion.getInActiveImageResource(), "drawable", "com.allryder.android"));
            titleTv.setText(suggestion.getTitle());
            checkBox = (ImageView) convertView.findViewById(R.id.favouriteIv);
            checkBox.setOnClickListener(this);
            checkBox.setId(position);
            if (suggestion.isFavourite()) {
                checkBox.setBackgroundResource(R.drawable.search_fav_active);
                checkBox.setTag(true);
            } else {
                checkBox.setTag(false);
            }
            break;
        case Suggestion.STATION_SUGGESTION_ID:
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.station_history_item, parent, false);
            }
            StationSuggestion stationSuggestion = (StationSuggestion) suggestion;
            StationView stationView = (StationView) convertView.findViewById(R.id.stationView);
            stationView.setStationTypes(stationSuggestion.getStationTypes());
            titleTv = (TextView) convertView.findViewById(R.id.titleTv);
            titleTv.setText(suggestion.getTitle());
            checkBox = (ImageView) convertView.findViewById(R.id.favouriteIv);
            checkBox.setOnClickListener(this);
            checkBox.setId(position);
            if (suggestion.isFavourite()) {
                checkBox.setBackgroundResource(R.drawable.search_fav_active);
                checkBox.setTag(true);
            } else {
                checkBox.setTag(false);
            }
            break;
        case Suggestion.CURRENT_LOCATION_SUGGESTION_ID:
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.current_location_item, parent, false);
            }
            titleTv = (TextView) convertView.findViewById(R.id.titleTv);
            titleTv.setText(suggestion.getTitle());
            break;
    }
    TextView detailTv = (TextView) convertView.findViewById(R.id.detailTv);
    detailTv.setText(suggestion.getDescription());
    convertView.setTag(type);
    return convertView;
}

然后将以下内容覆盖到方法,以便适配器知道应该将哪种视图类型传递到getview方法

@Override
public int getViewTypeCount() {
    return 4;
}

@Override
public int getItemViewType(int position) {
    return getItem(position).getTypeId();
}

但是我在这一行收到一个空指针

     checkBox.setOnClickListener(this);

因此没有实例化复选框。

你可以看到我在转换视图中添加了一个标记,这只是为了在转换视图不为null时检查标记是否等于类型。它总是如此。

为什么适配器找不到复选框?

0 个答案:

没有答案