我想知道这段代码有什么问题。我用 2 个 ArrayLists 填充 ListView,但它没有显示任何内容,是的,其中有数据,我有 2 个单独的 xml 来定义项目布局。这是我的适配器:
public class AchievementsAdapter extends BaseAdapter{
ArrayList<Achievement> achievements;
ArrayList<Statistic> stats;
Context context;
LayoutInflater vi;
achievementsHolder holderAch;
statisticHolder holderStats;
public AchievementsAdapter(Context context,
int simpleListItem1, ArrayList<Achievement> achs,
ArrayList<Statistic> stats) {
super();
this.achievements = achs;
this.context = context;
this.stats = stats;
System.out.println("Stats size: "+stats.size()+"Achievements size: "+achs.size());
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(convertView == null){
if(this.getItemViewType(position)==0){
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.achievement_layout, parent, false);
holderAch = new achievementsHolder();
view.setTag(holderAch);
}
else{
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.statistic_layout, parent, false);
holderStats = new statisticHolder();
view.setTag(holderStats);
}
}
else{
if(this.getItemViewType(position)==0){
achievementsHolder holderAch = (achievementsHolder) view.getTag();
Achievement ach = achievements.get(position);
holderAch.achievementsIcon = (ImageView)view.findViewById(R.id.imageAchievement);
holderAch.optionsText = (TextView)view.findViewById(R.id.textAchievement);
holderAch.achievementsIcon.setImageResource(0);
holderAch.optionsText.setText("");
if(ach.unlocked == true){
holderAch.achievementsIcon.setImageResource(R.drawable.unlockedach);
}
else{
holderAch.achievementsIcon.setImageResource(R.drawable.lockedach);
}
holderAch.optionsText.setText(ach.getTekst());
}
else{
statisticHolder holderStats = (statisticHolder) view.getTag();
holderStats.txtStat.setText("");
holderStats.txtProgr.setText("");
int temp = position-17;
final Statistic i = stats.get(temp);
holderStats.txtStat = (TextView)convertView.findViewById(R.id.statisticTxt);
holderStats.txtProgr= (TextView)convertView.findViewById(R.id.statisticProgress);
holderStats.txtStat.setText(i.getTekstStatistiek());
holderStats.txtProgr.setText(i.getProgress());
}
}
return view;
}
@Override
public int getItemViewType(int position) {
if(position > 17){
return 1;
} else {
return 0;
}
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 2;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return achievements.size() & stats.size() ;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
static class achievementsHolder
{
TextView optionsText;
ImageView achievementsIcon;
}
static class statisticHolder
{
TextView txtStat;
TextView txtProgr;
}
}
启动适配器:
AchievementsAdapter adapter = new AchievementsAdapter(this,R.layout.simple_list_item_1, achievements, stats);
listView = getListView();
listView.setAdapter(adapter);
提前致谢。