0
我有這個適配器如何通過映射值到適配器
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重視它越來越空,我有空指針exceptoin。
我調試它發現,當我打電話適配器和它達到super(inflater, rowLayoutID, headerID, itemHolderID, resizeMode);
,之後叫
@Override
protected int getSectionsCount() {
return mSponsorMap.size();
}
我'確保mSponsorMap = sponsormap還沒有被調用。爲什麼發生這種情況?
我嘗試了兩個代碼,但它仍然崩潰PLZ的幫助 – 2016-02-09 12:22:13