如何将映射值传递给适配器

时间:2014-08-29 09:38:20

标签: java android android-listview android-gridview android-adapter

我有这个适配器

public class BookcaseAdapter extends SectionableAdapter implements
        View.OnClickListener {

    private Activity activity;
    private Map<String, List<Sponsor>> mSponsorMap;

    public BookcaseAdapter(Activity activity, LayoutInflater inflater,
            int rowLayoutID, int headerID, int itemHolderID, int resizeMode, Map<String, List<Sponsor>> sponsorMap) {
        super(inflater, rowLayoutID, headerID, itemHolderID, resizeMode);
        this.activity = activity;
        mSponsorMap = sponsorMap;
    }

    @Override
    public Object getItem(int position) {
        for (int i = 0; i < mSponsorMap.size(); ++i) {
            if (position < mSponsorMap.get(i).size()) {
                return mSponsorMap.get(i).get(position);
            }
            position -= mSponsorMap.get(i).size();
        }
        // This will never happen.
        return null;
    }

    @Override
    protected int getDataCount() {
        int total = 0;
        for (int i = 0; i < mSponsorMap.size(); ++i) {
            total += mSponsorMap.get(i).size();
        }
        return total;
    }

    @Override
    protected int getSectionsCount() {
        return mSponsorMap.size();
    }


    @Override
    protected int getCountInSection(int index) {
        return mSponsorMap.get(index).size();
    }

    @Override
    protected int getTypeFor(int position) {
        int runningTotal = 0;
        int i = 0;
        for (i = 0; i < mSponsorMap.size(); ++i) {
            int sectionCount = mSponsorMap.get(i).size();
            if (position < runningTotal + sectionCount)
                return i;
            runningTotal += sectionCount;
        }
        // This will never happen.
        return -1;
    }

    @Override
    protected String getHeaderForSection(int section) {
        List<List<Sponsor>> l = new ArrayList<List<Sponsor>>(mSponsorMap.values());
        String string = l.get(section).get(0).getCountry_name();
        return string;
    }

    @Override
    protected void bindView(View convertView, int position) {
        String title = (String) getItem(position);
        ImageView label = (ImageView) convertView
                .findViewById(R.id.bookItem_title);
        label.setImageResource(R.drawable.place_holder_thumb);
        convertView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_SEARCH);
        ImageView label = (ImageView) v.findViewById(R.id.bookItem_title);
//      String text = label.getText().toString();
//      i.putExtra(SearchManager.QUERY, text);

//      activity.startActivity(i);
        label.setImageResource(R.drawable.place_holder_thumb);
    }

}

当我从我的活动中调用此适配器并传递给sponsorMap值时,它变为null并且我有空指针exceptoin。

我调试它发现当我调用适配器并且它到达super(inflater, rowLayoutID, headerID, itemHolderID, resizeMode);并且之后调用

@Override
    protected int getSectionsCount() {
        return mSponsorMap.size();
    }

我确定mSponsorMap = sponsormap没有被调用过。为什么会这样?

1 个答案:

答案 0 :(得分:0)

将调用它,但稍后,调用超级构造函数时获得的异常会阻止实际到达构造函数代码。

只需在覆盖方法中添加NPE防护:

@Override
protected int getSectionsCount() {
   return mSponsorMap == null ? 0 : mSponsorMap.size();
}

或者,您也可以将mSponsorMap初始化为空地图:

private Map<String, List<Sponsor>> mSponsorMap = Collections.emptyMap();

然后在你的构造函数中执行:

mSponsorMap.putAll(sponsorMap);

干杯,