0

我想知道这段代码有什么问题。我用 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);

提前致谢。

4

3 回答 3

0

您应该使用 setTag 和 getTag 来设置和获取先前的持有者视图。目前,未设置持有人。请检查修改后的代码。

 if(convertView == null){

    if(position <=17){
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.achievement_layout, parent, false);
        holderAch = new achievementsHolder();
        view.setTag(holderAch);
    }
    else{
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.statistic_layout, parent, false);
        holderStats = new statisticHolder();
        view.setTag(holderStats);
    }

}
else{

    if(position<=17){
        achievementsHolder holderAch = 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 = view.getTag();
        holderStats.txtStat.setText("");
        holderStats.txtProgr.setText("");
        int temp = position-17;
        final Statistic i = stats.get(temp);
        holderStats.txtStat = (TextView)view.findViewById(R.id.statisticTxt);
        holderStats.txtProgr= (TextView)view.findViewById(R.id.statisticProgress);
        holderStats.txtStat.setText(i.getTekstStatistiek());
        holderStats.txtProgr.setText(i.getProgress());
    }

}
于 2013-09-09T02:04:35.240 回答
0

在您的活动中,您需要将数据连接到列表视图。

IE

adapter= new PhotoGalleryAdapter(this, R.layout.photo_list, photos);
setListAdapter(adapter);
于 2013-09-09T01:55:34.427 回答
0

一些东西:

1)在您的构造函数中,您可能需要调用super(),或者只是为了安全起见。

2)您使用 ListView 生成两个不同视图的策略可能不起作用。ListView 维护从 getView() 返回的每个视图的视图类型,因为您没有getItemViewType(int position)在适配器中覆盖,所以您的 ListView 将传递一个之前使用的非空转换视图,一旦您传递了您的位置,它可能不是您想要的类型17.(很好,你已经覆盖了getViewTypeCount()

@Override
public int getItemViewType(int position) {
    if(position > 17){
        return 1;
    } else {
        return 0;
    }
}

3)您已经初始化了您的视图,但您还没有像 prijupaul 所说的那样设置setTag()/重用它们。getTag()此外,您需要使用findViewById()

4)见https://stackoverflow.com/a/3515221/2246704

于 2013-09-09T03:18:43.947 回答