具有2种不同布局的ListView不会显示任何内容

时间:2013-09-09 01:50:22

标签: android listview layout adapter

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

提前致谢。

3 个答案:

答案 0 :(得分:0)

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

adapter= new PhotoGalleryAdapter(this, R.layout.photo_list, photos);
setListAdapter(adapter);

答案 1 :(得分: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());
    }

}

答案 2 :(得分:0)

一些事情:

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

2)使用ListView生成两个不同视图的策略可能不起作用。 ListView维护从getView()返回的每个视图的视图类型,因为你没有覆盖适配器中的getItemViewType(int position),你的ListView将传递一个前面使用过的非null转换视图,它可能不是你想要的类型你通过了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