0

I wonder what is wrong about this code. I populate the ListView with 2 ArrayLists but it doesn't show anything and yes there is data in it and I have 2 seperate xml's to define the item layout. Here's my adapter :

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;
}

}

Launching adapter :

AchievementsAdapter adapter = new AchievementsAdapter(this,R.layout.simple_list_item_1, achievements, stats);
    listView = getListView();
    listView.setAdapter(adapter);

Thanks in advance.

David
  • 840
  • 6
  • 17
  • 37

3 Answers3

0

In your activity, you need to connect your data to your listview.

ie

adapter= new PhotoGalleryAdapter(this, R.layout.photo_list, photos);
setListAdapter(adapter);
Sebastian
  • 7,670
  • 5
  • 38
  • 50
Machee
  • 11
  • 5
0

You should use setTag and getTag to set and fetch the previous holder view. Currently, the holders are not set. Please check the modified code.

 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());
    }

}
prijupaul
  • 2,076
  • 2
  • 15
  • 17
0

A few things:

1) In your constructor, you likely need to call super(), or just do it to be safe.

2) Your strategy for generating two different views with a ListView probably won't work. ListView maintains view types for each view returned from getView(), since you aren't overriding getItemViewType(int position) in your adapter, your ListView will pass a non-null convertview used earlier that probably won't be the type you want once you pass your position of 17. (Its good that you've already overridden getViewTypeCount()

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

3) You've initialized your viewholders, but you haven't set them/reused them with setTag() / getTag() like prijupaul has said. Also, you need to assign values to those viewholders using findViewById()

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

Community
  • 1
  • 1
sddamico
  • 2,130
  • 16
  • 13
  • Did that but still no effect. I normally should get 24 entries but I am getting only 2 – David Sep 09 '13 at 10:22
  • `getCount()` appears to be using `&` instead of `+` it should probably look like: `@Override public int getCount() { return achievements.size() + stats.size() ; }` – sddamico Sep 11 '13 at 00:42